2009-11-29 148 views
1

我目前有两个表: 1.汽车(plate_number,品牌,CID) 2.借(开始日期,结束日期,品牌,ID)需要一个SQL查询

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

+4

听起来去吧作业问题。你的导师是否知道你在作弊? – 2009-11-29 05:22:51

+0

指定“可用”:对于给定日期? – mjv 2009-11-29 05:24:34

+1

你可能想要考虑用名词比如“loan”而不是动词“borrow”来命名你的表。表格通常代表实体(事物)。 – 2009-11-29 06:08:10

回答

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 

我没有尝试它,但它应该工作。

编辑:固定在哪里。

0

halfpseudo计算行或通过使用SQL计数或组,左也一同适用其中nullvalues

brand from car join borrow on borrow.brand=car.brand where endDate<currentdate 
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