WHERE s.id=sc.S_id
AND c.id=sc.C_id
AND sc.C_id=1
AND EXISTS (
SELECT sc.S_id FROM scores as sc WHERE scores.S_id =sc.S_id sc.C_id='2'
)
为什么我用mysql: SELECT sc.S_id FROM scores as sc WHERE scores.S_id =sc.S_id sc.C_id='2'
报错: 报错码:1054
Unknown column 'scores.S_id' in 'where clause'
建表语句如下:
CREATE TABLE Student
(
id INT(3) PRIMARY KEY NOT NULL AUTO_INCREMENT,
Sname VARCHAR(20) NOT NULL,
Sage INT(3) NOT NULL,
Ssex VARCHAR(10) NOT NULL
)
CREATE TABLE Course
(
id INT(3) PRIMARY KEY NOT NULL AUTO_INCREMENT,
Cname VARCHAR(20) NOT NULL,
T_id INT(3) NOT NULL
);
CREATE TABLE Scores
(
S_id INT(3) NOT NULL ,
C_id INT(3) NOT NULL,
score INT(3) NOT NULL
);
CREATE TABLE Teacher
(
id INT(3) PRIMARY KEY NOT NULL AUTO_INCREMENT,
Tname VARCHAR(20) NOT NULL
);
15 个解决方案
#1
SELECT sc.S_id FROM scores as sc WHERE
sc.S_id =sc.S_id sc.C_id='2'
当在from中定义了别名之后,在整个SELECT语句中原表名不可再用,要全部换成别名
当在from中定义了别名之后,在整个SELECT语句中原表名不可再用,要全部换成别名
#2
SELECT sc.S_id FROM scores as sc WHERE scores.S_id =sc.S_id
and sc.C_id='2'
#3
子查询有毛病:
SELECT S_id FROM scores WHERE scores.S_id =S_id and C_id='2'
子查询表的昵称不能和主查询表的昵称相同,否则不知道究竟是哪个表了.
SELECT S_id FROM scores WHERE scores.S_id =S_id and C_id='2'
子查询表的昵称不能和主查询表的昵称相同,否则不知道究竟是哪个表了.
#4
SELECT sc.S_id FROM scores as sc WHERE scores.S_id =sc.S_id and sc.C_id='2'
这个还是报那个错误;
换了别名的话就没有意义了。
求查询学过“1”并且也学过编号“2”课程的同学的学号、姓名 的
答案
这个还是报那个错误;
换了别名的话就没有意义了。
求查询学过“1”并且也学过编号“2”课程的同学的学号、姓名 的
答案
#5
你那个太混乱了,内外层的别名不能这样搞,人都看不懂哪个对应哪个,何况机器
SELECT *
FROM student s ,
course c ,
scores sc
WHERE s.id = sc.S_id
AND c.id = sc.C_id
AND sc.C_id = 1
AND EXISTS ( SELECT sc.S_id
FROM scores AS sc1
WHERE sc1.S_id = sc.S_id
AND sc1.C_id = '2' )
#6
为什么 单独运行SELECT sc.S_id
FROM scores AS sc1
WHERE sc1.S_id = sc.S_id
AND sc1.C_id = '2'
会报错
FROM scores AS sc1
WHERE sc1.S_id = sc.S_id
AND sc1.C_id = '2'
会报错
#7
因为EXISTS是关联性子查询,需要关联外表才能一起执行,非关联性子查询就不用
#8
试试这个
SELECT *
FROM student s ,
course c ,
scores sc
WHERE s.id = sc.S_id
AND c.id = sc.C_id
AND sc.C_id = 1
AND sc.s_id in ( SELECT sc1.S_id
FROM scores AS sc1
WHERE sc1.C_id = '2' )
#9
谢谢 学习到了
还请问
S_id score
1 1
2 2
3 3
变成 S_id score S_id score S_id score
1 1 2 2 3 3
还请问
S_id score
1 1
2 2
3 3
变成 S_id score S_id score S_id score
1 1 2 2 3 3
#10
----------------------------------------------------------------
-- Author :DBA_HuangZJ(发粪涂墙)
-- Date :2014-03-04 17:07:01
-- Version:
-- Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (X64)
-- Apr 2 2010 15:48:46
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)
--
----------------------------------------------------------------
--> 测试数据[huang]
if object_id('[huang]') is not null drop table [huang]
go
create table [huang]([S_id] int,[score] int)
insert [huang]
select 1,1 union all
select 2,2 union all
select 3,3
--------------生成数据--------------------------
declare @s nvarchar(4000)
set @s=''
Select @s=@s+','+quotename('S_id')+'=max(case when [S_id]='+quotename([S_id],'''')+' then [S_id] else 0 end)'
+','+quotename('score')+'=max(case when [score]='+quotename([S_id],'''')+' then [score] else 0 end)'
from [huang]
SET @s=SUBSTRING(@s,2,LEN(@s))
exec('select '+@s+' from [huang] ')
----------------结果----------------------------
/*
S_id score S_id score S_id score
----------- ----------- ----------- ----------- ----------- -----------
1 1 2 2 3 3
*/
#11
回复请引用。
#12
MYSQL行列转换不能这么做。应该用SUM(IF()...)
#13
谢谢,辛苦了。
#15
MYSQL行列转换不能这么做。应该用SUM(IF()...)
怎么用
怎么用
#1
SELECT sc.S_id FROM scores as sc WHERE
sc.S_id =sc.S_id sc.C_id='2'
当在from中定义了别名之后,在整个SELECT语句中原表名不可再用,要全部换成别名
当在from中定义了别名之后,在整个SELECT语句中原表名不可再用,要全部换成别名
#2
SELECT sc.S_id FROM scores as sc WHERE scores.S_id =sc.S_id
and sc.C_id='2'
#3
子查询有毛病:
SELECT S_id FROM scores WHERE scores.S_id =S_id and C_id='2'
子查询表的昵称不能和主查询表的昵称相同,否则不知道究竟是哪个表了.
SELECT S_id FROM scores WHERE scores.S_id =S_id and C_id='2'
子查询表的昵称不能和主查询表的昵称相同,否则不知道究竟是哪个表了.
#4
SELECT sc.S_id FROM scores as sc WHERE scores.S_id =sc.S_id and sc.C_id='2'
这个还是报那个错误;
换了别名的话就没有意义了。
求查询学过“1”并且也学过编号“2”课程的同学的学号、姓名 的
答案
这个还是报那个错误;
换了别名的话就没有意义了。
求查询学过“1”并且也学过编号“2”课程的同学的学号、姓名 的
答案
#5
你那个太混乱了,内外层的别名不能这样搞,人都看不懂哪个对应哪个,何况机器
SELECT *
FROM student s ,
course c ,
scores sc
WHERE s.id = sc.S_id
AND c.id = sc.C_id
AND sc.C_id = 1
AND EXISTS ( SELECT sc.S_id
FROM scores AS sc1
WHERE sc1.S_id = sc.S_id
AND sc1.C_id = '2' )
#6
为什么 单独运行SELECT sc.S_id
FROM scores AS sc1
WHERE sc1.S_id = sc.S_id
AND sc1.C_id = '2'
会报错
FROM scores AS sc1
WHERE sc1.S_id = sc.S_id
AND sc1.C_id = '2'
会报错
#7
因为EXISTS是关联性子查询,需要关联外表才能一起执行,非关联性子查询就不用
#8
试试这个
SELECT *
FROM student s ,
course c ,
scores sc
WHERE s.id = sc.S_id
AND c.id = sc.C_id
AND sc.C_id = 1
AND sc.s_id in ( SELECT sc1.S_id
FROM scores AS sc1
WHERE sc1.C_id = '2' )
#9
谢谢 学习到了
还请问
S_id score
1 1
2 2
3 3
变成 S_id score S_id score S_id score
1 1 2 2 3 3
还请问
S_id score
1 1
2 2
3 3
变成 S_id score S_id score S_id score
1 1 2 2 3 3
#10
----------------------------------------------------------------
-- Author :DBA_HuangZJ(发粪涂墙)
-- Date :2014-03-04 17:07:01
-- Version:
-- Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (X64)
-- Apr 2 2010 15:48:46
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)
--
----------------------------------------------------------------
--> 测试数据[huang]
if object_id('[huang]') is not null drop table [huang]
go
create table [huang]([S_id] int,[score] int)
insert [huang]
select 1,1 union all
select 2,2 union all
select 3,3
--------------生成数据--------------------------
declare @s nvarchar(4000)
set @s=''
Select @s=@s+','+quotename('S_id')+'=max(case when [S_id]='+quotename([S_id],'''')+' then [S_id] else 0 end)'
+','+quotename('score')+'=max(case when [score]='+quotename([S_id],'''')+' then [score] else 0 end)'
from [huang]
SET @s=SUBSTRING(@s,2,LEN(@s))
exec('select '+@s+' from [huang] ')
----------------结果----------------------------
/*
S_id score S_id score S_id score
----------- ----------- ----------- ----------- ----------- -----------
1 1 2 2 3 3
*/
#11
回复请引用。
#12
MYSQL行列转换不能这么做。应该用SUM(IF()...)
#13
谢谢,辛苦了。
#14
#15
MYSQL行列转换不能这么做。应该用SUM(IF()...)
怎么用
怎么用