为了账号安全,请及时绑定邮箱和手机立即绑定

请教这个sql怎么写?

请教这个sql怎么写?

阿波罗的战车 2018-07-20 11:23:25
现在商品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 回答

?
慕少森

TA贡献2019条经验 获得超9个赞

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;


查看完整回答
反对 回复 2018-07-23
  • 1 回答
  • 0 关注
  • 548 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信