如何通过JOIN从另一个表中查找不存在的数据? [英] How to find non-existing data from another Table by JOIN?

查看:124
本文介绍了如何通过JOIN从另一个表中查找不存在的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表TABLE1看起来像:

I have two tables TABLE1 which looks like:

id      name     address
1       mm     123
2       nn     143

和带有w/c的TABLE2看起来像:

and TABLE2 w/c looks like:

name     age
mm      6
oo      9

我想通过将TABLE1TABLE2进行比较来获得不存在的名称.

I want to get the non existing names by comparing the TABLE1 with the TABLE2.

所以基本上,我必须获得第二行,w/c具有TABLE2中不存在的NN名称,输出应如下所示:

So basically, I have to get the 2nd row, w/c has a NN name that doesn't exist in the TABLE2, the output should look like this:

id      name     address
2      nn      143

我已经尝试过了,但是不起作用:

I've tried this but it doesn't work:

SELECt  w.* FROM TABLE1 W INNER JOIN TABLE2 V
  ON W.NAME <> V.NAME

并且仍在获取现有记录.

and it's still getting the existing records.

推荐答案

INNER JOIN在这里无济于事.

解决此问题的一种方法是使用LEFT JOIN:

One way to solve this is by using a LEFT JOIN:

SELECT w.* 
FROM TABLE1 W 
LEFT JOIN TABLE2 V ON W.name = V.name
WHERE ISNULL(V.name);

这篇关于如何通过JOIN从另一个表中查找不存在的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆