gpt4 book ai didi

mysql - 获取表中不可用的数据

转载 作者:行者123 更新时间:2023-12-01 00:35:27 25 4
gpt4 key购买 nike

================= Customer Table =============
# customer
Code Description
301 Customer 1
302 Customer 2
386 Customer 3
387 Customer 4
390 Customer 5
391 Customer 6
392 Customer 7

我正在使用下面的查询

select * from customer
where code not in (310, 350, 301, 302);

从上面的查询中,它将获取以下数据

Code    Description 
386 Customer 3
387 Customer 4
390 Customer 5
391 Customer 6
392 Customer 7

但实际上我想要输出为

310 
350

表示哪些数据不可用,我们将其放在 where 条件中。

我不想创建另一个表来实现这一点。

请分享一些想法。

最佳答案

使用子查询创建具有这些值的派生表,然后将其 LEFT JOIN 到真实表。

SELECT t1.code
FROM (SELECT 310 AS code UNION SELECT 350 UNION SELECT 301 UNION SELECT 302) AS t1
LEFT JOIN customer AS c ON c.code = t1.code
WHERE c.code IS NULL

DEMO

关于mysql - 获取表中不可用的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52380604/

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