请教这个sql怎么写?

现在商品item表结构
item_id, title, product_id, type, price

一个product下有多个商品,商品item有4种type(0,1,2,3),同一个type下可以有多个同样type与同样product_id的商品。

现在有N个商品id,我要根据这些商品id,来获取这些商品的product_id,然后根据这些product_id再获取item表里面,所有 product_id与4个type值组合下的一个price最低的商品 的列表。

应该怎么写sql?

阅读 1.7k
1 个回答
select aa.* 
from item aa 
join (
    select a.product_id,a.type,min(a.price) price 
    from item a 
    join (select product_id from item where item_id in (N) ) b
    on a.product_id=b.product_id
    group by product_id,type) bb
on aa.product_id=bb.product_id and aa.type=bb.type and aa.price=bb.price;
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题