简体   繁体   English

SQL-另一个表中不存在SELECT行

[英]SQL - SELECT rows NOT EXISTS in another table

I'm trying to select the rows not present in table B , based on table A . 我试图基于表A选择表B不存在的行。

Unlike table B , table A has "_00" at the end of the titleid, and the column is called title instead of titleid . 与表B不同,表A在titleid的末尾具有“ _00”,并且该列称为title而不是titleid

Table A : 表A

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

Table B : 表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 ? 如何选择A中存在但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 . 您要从Table A提取数据,对Table B进行left join ,并where TableB.Title is null提取数据。

Your Query was trying to pull data where TableA.Title is NULL . 您的查询试图where TableA.Title is NULL提取数据。

You need to LEFT JOIN tableb instead if tablea 如果tablea则需要LEFT JOIN tableb代替

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 这将显示tablea哪些记录在tableb不匹配并且为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 )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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