匹配来自不同表格的两个列 - MYSQL PHP

时间:2015-03-26 08:07:15

标签: mysql

我在mysql中有两个表

表1是车辆 有所有车辆的记录

表2是贡献 有车辆的每日捐款记录

我在两个表中都有一个名为regno的列

我想要一个显示不在贡献中的车辆注册的查询

我尝试了什么

select vehicles.regno from vehicles,contributions where vehicles.regno<>contributions.regno  ORDER BY vehicles.regno;

请帮忙。提前谢谢

3 个答案:

答案 0 :(得分:1)

试试这个:

SELECT V.regno FROM vehicles AS V LEFT JOIN contributions AS C ON (V.regno = C.regno) WHERE C.regno IS NULL

答案 1 :(得分:0)

  select regno from vehicles where regno not in (select  regno from contributions);

答案 2 :(得分:0)

您可以使用左连接来获得结果。

select v.regno
from vehicles v
left join contributions c on c.regno = v.regno //A LEFT JOIN produces a set of records which matches every entry in the left table (user) regardless of any matching entry in the right table
where c.regno is null// This will filter out results where right table entry is not available.
ORDER BY vehicles.regno;