表中不存在返回的记录 [英] Return not exists record in the table

查看:79
本文介绍了表中不存在返回的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取表中没有任何记录的ID?例如,

How to get the IDs which doesn't have any record in the table? Like for example,

select id,name,mail from users where id in(2,3,4,5,6)

查询返回记录2、3、4的输出,但不返回5和6的输出,因为表中没有记录.

The query returns output for the record 2,3,4 but not 5 and 6 as there is no record exists in the table.

现在我想知道表中没有记录的id是什么.有什么办法可以在mysql中获得此输出?

Now i want to know what are the ids that doesn't have record in the table. Is there any way to get this output in mysql?

推荐答案

尝试:

SELECT id
FROM (
  SELECT 2 as id
  union 
  SELECT 3
  union 
  SELECT 4
  union 
  SELECT 5
  union 
  SELECT 6
) q
WHERE q.id NOT IN ( SELECT id FROM users )

这篇关于表中不存在返回的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆