我想写出租车的次数,我不能让它上班,我有两张桌子。一个称为vehicle
,另一个称为rental
。
租赁是所有"租赁"当你预订一辆车时,所有车辆都存放在车内。这是我所做的几乎有效的查询。
SELECT vehicle.id,COUNT(rental.vid) as rented,IFNULL(rental.vid,0) as nothing, vehicle.make as make, vehicle.model as model, vehicle.regnr as regnr, vehicle.color as color, vehicle.state as state, vehicle.imgurl as img, vehicle.description as description, vehicle.id
FROM rental,vehicle
WHERE vid = vehicle.id GROUP BY vid
并将打印出来:
没有价值(从未租过)的其余部分不存在,我已经尝试了很多不同的方法IFNULL
但是没有任何地方可以获得。< / p>
答案 0 :(得分:1)
Select
从Vehicle
表和Left Join
到Rental
表。这将包括从未租用的车辆及其计数(rental.vid)将为0:
SELECT vehicle.id
,COUNT(rental.vid) as rented
, vehicle.make as make
, vehicle.model as model
, vehicle.regnr as regnr
, vehicle.color as color
, vehicle.state as state
, vehicle.imgurl as img
, vehicle.description as description
FROM vehicle
left join rental on vid = vehicle.id
GROUP BY vehicle.id
您的示例中的隐式联接等同于inner join
。使用左连接,可以从源表中选择所需的所有行。如果您要加入的表格匹配,它们也会出现。这是将数据附加到源表的好方法。