请教这个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?


慕后森
浏览 586回答 1
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) bbon aa.product_id=bb.product_id and aa.type=bb.type and aa.price=bb.price;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

MySQL