【问题标题】:Select from two table where value not in nother table in MySQL从两个表中选择,其中值不在 MySQL 的另一个表中
【发布时间】:2019-03-11 09:53:28
【问题描述】:

我有两个表 table1 和 table2。我想从 table1 中选择,其中 id 不在 table2 id 中。

table1
id     name
001    Testing
002    Hello
003    World
004    Programmer

table2
id     name
001    Testing
003    World

我的期望

id     name
002    Hello
004    Programmer

我试试这些代码

SELECT * FROM table1 as a where a.id <> (SELECT b.id from table2 as b )

有什么解决办法吗?

【问题讨论】:

标签: mysql


【解决方案1】:

您可以尝试使用JOIN

SELECT * FROM table1 a left join table2 b on a.id=b.id
where b.id is null

【讨论】:

  • 非常感谢
【解决方案2】:

使用NOT IN 更改您的查询,如下所示。

SELECT * FROM table1 as a where a.id NOT IN (SELECT b.id from table2 as b )

【讨论】:

    【解决方案3】:

    你可以不在

    SELECT * FROM table1 as a where a.id NOT IN (SELECT b.id from table2 as b )
    

    源:https://www.w3resource.com/mysql/comparision-functions-and-operators/not-in.php

    【讨论】:

    • 非常感谢
    猜你喜欢
    • 2012-03-04
    • 1970-01-01
    • 2012-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-28
    相关资源
    最近更新 更多