SQL: Select Keys that doesn't exist in one table

前端 未结 6 705
刺人心
刺人心 2021-01-11 11:24

I got a table with a normal setup of auto inc. ids. Some of the rows have been deleted so the ID list could look something like this:

(1, 2, 3, 5, 8,

6条回答
  •  北海茫月
    2021-01-11 12:11

    I have the same problem: I have a list of values from the user, and I want to find the subset that does not exist in anther table. I did it in oracle by building a pseudo-table in the select statement Here's a way to do it in Oracle. Try it in MySQL without the "from dual":

    -- find ids from user (1,2,3) that *don't* exist in my person table
    -- build a pseudo table and join it with my person table
    select pseudo.id from (
      select '1' as id from dual
      union select '2' as id from dual
      union select '3' as id from dual
    ) pseudo
    left join person
      on person.person_id = pseudo.id
    where person.person_id is null
    

提交回复
热议问题