gpt4 book ai didi

mysql - SQL where 子句中的否定

转载 作者:行者123 更新时间:2023-11-29 11:37:22 25 4
gpt4 key购买 nike

嗨,我正在尝试解决否定数据的问题,我有两个表

类(class):

+----+----------+--+
| id | name | |
+----+----------+--+
| 1 | 'First' | |
| 2 | 'Second' | |
+----+----------+--+

列表:

+----+----------+--+
| id | id_class | |
+----+----------+--+
| 1 | 1 | |
+----+----------+--+

类id引用id_class

我想选择不在列表中的数据。我试过这个:

  SELECT c.name FROM classes c JOIN lists l ON (l.id_class=c.id) WHERE l.id_class!=c.id 

但是没有结果,猜测不正确 有解决方案吗?

最佳答案

您需要一个LEFT JOIN:

SELECT c.id, c.name
from classes c
left join lists l on (l.id_class=c.id)
where l.id_class is null

或者,您也可以使用NOT EXISTS:

select c.id, c.name
from classes c
where not exists (select *
from lists l
where l.id_class=c.id)

关于mysql - SQL where 子句中的否定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36363935/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com