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

标签 mysql sql

================= 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/

相关文章:

mysql - 最便宜的 SQL 语句可能/是否有客户端 SQL 语句?

sql - 日期时间函数 : semester, 季度

mysql - SQL:如何过滤以仅返回结果集中的行,其中字段中的某些数据出现在未过滤结果集中的多行中?

php - 如何在mysql查询中找到2个日期的总小时数

sql - 与生产环境相比,有什么好的方法可以快速审核源代码控制中的代码吗?

php - 将 php 数组传递给 sql 查询

sql - 试图将重复的行合并在一起,多次指定最大值错误

php - 如何在 php 中创建我的帐户页面

php - 准备好的语句中的递归

mysql - 检查多列中的多个值,sql select