需要一个SQL查询

时间:2009-11-29 05:20:47

标签: sql mysql

我目前有两张桌子: 汽车(plate_number,品牌,cid) 借用(StartDate,endDate,brand,id)

我想写一个查询,以获取每个品牌的所有可用品牌和可用汽车数量

3 个答案:

答案 0 :(得分:0)

SELECT c.Brand COUNT(plate_number) FROM car as c LEFT JOIN (borrow as b) on c.cid = b.id WHERE endDate < NOW() GROUP BY c.Brand

我没有尝试,但应该可以。

编辑:修复在哪里。

答案 1 :(得分:0)

halfpseudo count行或使用sql count或group by,left join也适用于nullvalues

brand from car join borrow on borrow.brand=car.brand where endDate<currentdate

答案 2 :(得分:0)

表格:

car(plate_number, brand, cid)

borrow(StartDate, endDate, brand, id)

select c.plate_number, b.brand, count(b.brand) as available_number
from   car c, borrow b 
where  c.brand=b.brand
       and end_date>=sysdate 
having count(1)>0
group by c.plate_number, b.brand