我有一个订单和order_items表:
Order
-id
Items
-id
-order_id
-date_added
-price
除了以下内容之外,我还想将MAX date_added值作为item的列返回。我怎么能这样做?
select item.id, count(*)
from orders o
inner join items i on o.id = i.order_id
group by item.id
order by count(*) desc
答案 0 :(得分:0)
只需使用MAX功能
即可select item.id, count(*), MAX(date_added)
from orders o
inner join items i on o.id = i.order_id
group by item.id
order by count(*) desc
答案 1 :(得分:0)
这个简单的查询应该为你做到:
select item.id, count(*) count_of_orders, max(date_added ) max_date_added
from orders o
inner join items i on o.id = i.order_id
group by item.id
order by count(*) desc