[英]join two tables and select records only if NOT in table 2
I would like to join two tables and only print records from table 1 where the rec_number is NOT in table 2. 我想加入两个表,只打印表1中rec_number不在表2中的记录。
table 1
name rec_number
john smith 123
Bob jonson 345
etc
Table 2
Bob jonson 345
etc
What is the query in php that would do this so the query only gives me John smith, not bob jonson. 什么是PHP中的查询会这样做,所以查询只给我约翰史密斯,而不是鲍勃jonson。 is it:
是吗:
$query = "select * from table1
left join rec_number on table1.rec_number = table2.rec_number";
$result=mysql_query($query);
Thank you. 谢谢。
You can use this query 您可以使用此查询
select
t1.*
from table1 t1
left join table2 t2
on t2.rec_number = t1.rec_number
where t2.rec_number IS NULL
除了Abhik提到的left join
之外,您还可以使用子选择:
SELECT * FROM table1 WHERE table1.name NOT IN (SELECT name FROM table2);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.