黄聪:MYSQL提交一批ID,查询数据库中不存在的ID并返回

时间:2022-01-30 23:07:22

假设你数据库有个A表:

    ID NAME
    1 aaa
    2 bbb
    3 ccc
    4 ddd


需求:给你几个ID,返回A表中不存在的ID? 例如提交1,2,8,9 返回8,9

 

select B.id
from (select 1 as id from dual
union
select 2 as id from dual
union
select 8 as id from dual
union
select 9 as id from dual
) B
left join A
on A.id = B.id
where A.id is null;