日期:2014-05-18  浏览次数:20358 次

请教数据库查找出错
SQL code
SELECT C_Score,S_Name,S_ID,E_Title 
FROM Scores,Teachers,Students,Essay 
WHERE T_ID='1' and 
Teachers.T_Group=Students.S_Group and 
Students.S_ID=Essay.E_SID not Exists
{SELECT  * FROM Scores,Teachers,Students WHERE
Scores.C_TID=Teachers.T_ID and 
Scores.C_SID=Students.S_ID)


上面的代码出错了。。出现消息 156,级别 15,状态 1,第 5 行
关键字 'not' 附近有语法错误。
消息 102,级别 15,状态 1,第 8 行
')' 附近有语法错误。

我想实现的是排除了查找到的数据中Scores.C_TID=Teachers.T_ID and Scores.C_SID=Students.S_ID的情况,如果我的有问题请问应该怎么做呢?


------解决方案--------------------
and not exists

后面接圆括号,不是花括号。
------解决方案--------------------
SELECT C_Score,S_Name,S_ID,E_Title 
FROM Scores,Teachers,Students,Essay 
WHERE T_ID='1' and 
Teachers.T_Group=Students.S_Group and 
Students.S_ID=Essay.E_SID not Exists
(SELECT * FROM Scores,Teachers,Students WHERE
Scores.C_TID=Teachers.T_ID and 
Scores.C_SID=Students.S_ID)

红色部分应该是现在这样,而不是“{”
------解决方案--------------------
not 前面加 and
------解决方案--------------------
SQL code

--这种语法上就应该没有问题了,但是估计你也很难得到你想要的结果。
SELECT 
C_Score,S_Name,S_ID,E_Title 
FROM Scores,Teachers,Students,Essay 
WHERE T_ID='1' and Teachers.T_Group=Students.S_Group 
and Students.S_ID=Essay.E_SID 
and not Exists --少个and
( --之前括号确实不对
SELECT  * FROM Scores,Teachers,Students,Essay WHERE --这个位置少个表
Scores.C_TID=Teachers.T_ID and Scores.C_SID=Students.S_ID
)

------解决方案--------------------
SQL code

--> 测试数据: @a
declare @a table (id int)
insert into @a
select 1 union all
select 2 union all
select 3


--> 测试数据: @b
declare @b table (col int)
insert into @b
select 2 union all
select 3 union all
select 4

--> 测试数据: @c
declare @c table (c int)
insert into @c
select 4 union all
select 5 union all
select 6

--> 测试数据: @d
declare @d table (d int)
insert into @d
select 3 union all
select 4 union all
select 5

--四个表内连接,很容易没有结果的
select * from @a a,@b b,@c c,@d d
where not exists
(select * from @a a,@b b,@c c,@d d where b.col=c.c)

------解决方案--------------------
你把exists理解成in了

按照你的意思用except就应该可以了。