【问题标题】:join two tables and select records only if NOT in table 2仅当不在表 2 中时才连接两个表并选择记录
【发布时间】:2014-06-14 13:20:04
【问题描述】:

我想连接两个表,只打印表 1 中 rec_number 不在表 2 中的记录。

table 1
name        rec_number
john smith   123
Bob jonson   345
etc

Table 2 
Bob jonson   345
etc

什么是 php 中的查询可以做到这一点,所以查询只给我 John smith,而不是 bob jonson。 是吗:

    $query = "select * from table1
    left join rec_number on table1.rec_number = table2.rec_number";
    $result=mysql_query($query);

谢谢。

【问题讨论】:

标签: php mysql


【解决方案1】:

你可以使用这个查询

select 
t1.*
from table1 t1
left join table2 t2 
on t2.rec_number = t1.rec_number
where t2.rec_number IS NULL

【讨论】:

【解决方案2】:

除了 Abhik 提到的left join,您还可以使用子选择:

SELECT * FROM table1 WHERE table1.name NOT IN (SELECT name FROM table2);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-31
    • 1970-01-01
    • 2019-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-02
    • 1970-01-01
    相关资源
    最近更新 更多