简体   繁体   中英

SQL - SELECT rows NOT EXISTS in another table

I'm trying to select the rows not present in table B , based on table A .

Unlike table B , table A has "_00" at the end of the titleid, and the column is called title instead of titleid .

Table A :

id | titleid
---+----------
1  | TEST1_00
2  | TEST2_00
3  | TEST3_00
4  | TEST4_00

Table B :

id | title
---+-------
1  | TEST1
2  | TEST2

I currently have:

SELECT `t1.titleid`
FROM `tableb t1`
LEFT JOIN `tablea t2` ON `t2.title + '_00' = t1.titleid`
WHERE `t2.title` IS NULL

How can I select the values which are present in A but not in B ?


Desired output

id | title
---+----------
3  | TEST3_00
4  | TEST4_00
 SELECT t1.titleid
 FROM tablea t1
 LEFT JOIN tableb t2 ON t2.title + '_00' = t1.titleid
 WHERE t2.title IS NULL

You want to pull Data from Table A , do a left join on Table B and pull data where TableB.Title is null .

Your Query was trying to pull data where TableA.Title is NULL .

You need to LEFT JOIN tableb instead if tablea

SELECT `t1.titleid`
FROM `tablea t1`
LEFT JOIN `tableb t2` ON `t1.titleid = t2.title+ '_00'`
WHERE `t2.title` IS NULL

This will show which records in tablea don't have a match in tableb and are null

It is possible to do this like that

SELECT `t1.titleid`
FROM `tablea t1`

WHERE 
NOT EXISTS (SELECT t2.title FROM `tableb t2` WHERE `t1.titleid = t2.title+ '_00'`)
select * from A where SUBSTRING(A.title,0, 6) in (select B.title from B )

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