[英]Mysql Query Inner Join
两张桌子
tblEatables和tblConfirm_Eatables
tblEatables
Sno Name 1 Apples 2 Oranges 3 Papaya 4 Jackfruit 5 Pineapple 6 Mango
tblConfirm_Eatables
Eatbles_Id Confirm_Status 1 0 2 1 3 0 4 0
问题1
为什么下面的查询带来了重复的记录集
SELECT E.Name
FROM Eatables E INNER JOIN
Confirm_Eatables CE ON CE.Eatbles_Id != E.Sno
虽然我使用<>,但为什么下面的查询带来了表中的所有食物
SELECT E.Name
FROM Eatables E INNER JOIN
Confirm_Eatables CE ON E.Sno != CE.Eatbles_Id
GROUP BY E.Name
OP
Name Apples Jackfruit Mango Oranges Papaya Pineapple
页面顶部有很多不同JOIN的例子http://www.sql-tutorial.ru/en/book_explicit_join_operations/page1.html
对我来说,最好在连接查询中使用=并在它之后使用除外。 按你的方式:
SELECT E.Name FROM Eatables
EXCEPT ALL
SELECT E.Name FROM Eatables E INNER JOIN
Confirm_Eatables CE ON CE.Eatbles_Id = E.Sno
我想你想要的是:
SELECT a.Name
FROM tblEatables a
LEFT JOIN tblConfirm_Eatables b ON a.Sno = b.Eatables_Id
WHERE b.Eatables_Id IS NULL
这将获得tblEatables
中不在tblConfirm_Eatables
所有行。
为了更好地理解其工作原理,请参阅此链接以获得在SQL中使用连接的良好可视指南; 特别是第四个,我们使用LEFT JOIN
/ IS NULL
技术来检索表A中表B中没有匹配的所有记录。
你应该在WHERE
子句中使用这个E.Sno != CE.Eatbles_Id
条件。
在连接中使用!=
运算符会给您带来意想不到的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.