0

下面是我创建的表的副本。我的目标是简单地id_num从第一张桌子中挑选出第二张桌子上找不到的独特的东西。

我试过做下面的代码,但不知何故,我一直得到空的结果

SELECT `first_table`.name FROM `first_table`
INNER JOIN `second_table`
ON `first_table`.id_num = `second_table`.id_num
WHERE `first_table`.name = `second_table`.name 

第一张表:

   id_num   |  name
    301     |  Bobby
    123     |  George
    25      |  Vicky

第二张表:

   id_num   |  name
    301     |  Bobby
    435     |  George
    25      |  Vicky

我正在寻找的理想结果:

id_num   |  name
435     |  George
4

2 回答 2

3

LEFT JOIN应该在这里工作。

SELECT `first_table`.name FROM `first_table`
LEFT JOIN `second_table`
ON `first_table`.id_num = `second_table`.id_num
WHERE `second_table`.id_num is NULL

另请参阅此有用的信息图

在此处输入图像描述

于 2013-10-13T10:37:18.050 回答
1

试试这个NOT IN

select `id_num` , name from `table2` where name not in (
                                                  SELECT t1.name FROM `table1` t1
                                                  INNER JOIN `table2` t2
                                                  ON t1.id_num = t2.id_num )

在这里演示

于 2013-10-13T10:40:30.610 回答