请教数据库查找出错
- 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 WHEREScores.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
--> 测试数据: @adeclare @a table (id int)insert into @aselect 1 union allselect 2 union allselect 3--> 测试数据: @bdeclare @b table (col int)insert into @bselect 2 union allselect 3 union allselect 4--> 测试数据: @cdeclare @c table (c int)insert into @cselect 4 union allselect 5 union allselect 6--> 测试数据: @ddeclare @d table (d int)insert into @dselect 3 union allselect 4 union allselect 5--四个表内连接,很容易没有结果的select * from @a a,@b b,@c c,@d dwhere not exists(select * from @a a,@b b,@c c,@d d where b.col=c.c)
[解决办法]
你把exists理解成in了
按照你的意思用except就应该可以了。