简体   繁体   中英

SQL I have two table which join with foreign key . I want to find out missing id from second table

This two table are join with foreign key. I insert some value in info but i need to find out which value are not inserted into info from name table. I mean find name.id is not equal info.sid

在此处输入图片说明

在此处输入图片说明

The normative approach is an anti-join pattern.

To find rows in table1 which have an value in the id column which does not have a matching row in table2 (ie there are no rows in table2 with fk_id value that match)

 SELECT t1.id
   FROM table1 t1
   LEFT
   JOIN table2 t2
     ON t2.fk_id = t1.id
  WHERE t2.fk_id IS NULL

This query basically says return all rows from table1, along with any matching rows from table2, but exclude rows where we found a matching row in table2. Leaving us with only rows from table1 that didn't have a match in table2.

This is a demonstration of just one of the approaches. There are a couple of other query patterns that will return an equivalent result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM