![](/img/trans.png)
[英]Find all rows from a table which don't have references in any other tables from the same database
[英]SQL Need to select rows that don't have same spelling or don't match joined from 2 different tables
我正在尝试返回每行中名称不同或在 SQL 中不匹配的列。 我尝试过寻找东西,但找不到正确的解决方案,因为任何类似的东西都在寻找同一张表中不匹配的结果。
例如,这是我将要使用的。
表1.ID | table1.name | table2.name |
---|---|---|
伊利诺伊州 | 尼娜 | 尼娜 |
伊利诺伊州 | 麦克风 | 麦克风 |
威斯康星 | 乔恩 | 乔恩 |
AZ | 埃里克 | 埃里克 |
AZ | 亚历克斯 | NULL |
德克萨斯州 | NULL | 汤姆 |
德克萨斯州 | 伊恩 | 伊恩 |
我想返回拼写不同的 Nina、Jon 的、带有 Null 的 Alex 和 Null/Tom
在 MySQL
我认为您可以尝试 SOUNDEX(expr) 或 SOUNDS LIKE 之类的东西。 你会想要索引它。
它会是这样的:
select A.id, A.name as A_name, B.name as B_name
from table1 as A
inner join table2 as B
on A.name SOUNDS LIKE B.name
declare @table1 as table(ID varchar(2), name varchar(20))
declare @table2 as table(name varchar(20))
insert into @table1(ID,name) values('IL','Nina') ,('IL','Mike'),('WI','Jon')
,('AZ','Eric'),('AZ','Alex'),('TX',NULL),('TX','Ian')
insert into @table2(name) values('Niña'),('Mike'),('Jonn'),('Eric'), (NULL),('Tom'),('Ian')
select tb1.ID,tb1.name,tb2.name name_tb2 from @table1 tb1 right join @table2 tb2
on soundex(tb1.name) like soundex(tb2.name)
output
ID name name_tb2
NULL NULL Niña
IL Mike Mike
WI Jon Jonn
AZ Eric Eric
NULL NULL NULL
NULL NULL Tom
TX Ian Ian
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.