MySQL棘手的选择混合找到行和正常选择

时间:2016-08-12 19:43:46

标签: mysql sql select

我有一张名为delivery的表 -

id | driver_id | depart_loc | arrive_loc | depart_time | arrive_time

然后..我有另一张关于交付项目的表格

id | delivery_id | item_id | serial

如何选择交付和项目表,以便它就像这样

driver_id | arrive_loc | arrive_time | total_item_found_in_delivery

甚至可能吗?

1 个答案:

答案 0 :(得分:1)

我设法完成了它。以下选择做了诀窍(好吧......或多或少)

SELECT driver_id, arrive_loc, arrive_time, itemcount 
FROM (SELECT delivery.driver_id as driver_id, COUNT(items.id) AS itemcount, delivery.arrive_loc as arrive_loc, 
delivery.arrive_time as arrive_time 
FROM delivery 
LEFT JOIN items
 ON delivery.id = items.delivery_id 
GROUP BY delivery.arrive_loc) AS A

Thanx家伙!