gpt4 book ai didi

mysql - 如果在另一个表上匹配,则排除一个表中的行 - MySql

转载 作者:行者123 更新时间:2023-11-29 15:29:23 26 4
gpt4 key购买 nike

我有两张 table :

第一个名为类别,包含 3 列“管理员、ID、名称”。

administrators | id  | name
1 | 23 | David
2 | 24 | Jemmy
3 | 25 | Frank
NULL | 26 | GLOBAL1
NULL | 27 | GLOBAL2
NULL | 28 | GLOBAL3

第二个表名为disabledcategories,包含两列“administrators”和“id”。

administrators | id 
1 | 26 > ( GLOBAL1 )
1 | 27 > ( GLOBAL2 )

2 | 26 > ( GLOBAL1 )
2 | 27 > ( GLOBAL2 )

3 | 26 > ( GLOBAL1 )
3 | 27 > ( GLOBAL2 )
3 | 28 > ( GLOBAL3 )

我想从禁用类别中没有的类别中选择“管理员= LIKE 1 OR NULL”

所需的输出如下:

administrators | id  | name
1 | 23 | David
NULL | 28 | GLOBAL3

我该怎么做?提前非常感谢您!

最佳答案

我想你想要:

select c.*
from categories c
where not exists (select 1
from disabledcategories dc
where dc.id = c.id
) or
c.administrators = 1 or
c.administrators is null;

但是,对于您的示例结果,不需要进行查找:

select c.*
from categories c
where c.administrators = 1 or
c.administrators is null;

关于mysql - 如果在另一个表上匹配,则排除一个表中的行 - MySql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58841772/

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