[英]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.