MySQL Left Join + Min

前端 未结 4 1274
闹比i
闹比i 2020-12-14 08:52

Seemingly simple MySQL question, but I\'ve never had to do this before..

I have two tables, items and prices, with a one-to-many relationship.

Items         


        
4条回答
  •  抹茶落季
    2020-12-14 09:01

    This will return multiple records for a record in Items if there are multiple Prices records for it with the minimum price:

    select items.id, items.name, prices.price, prices.id
    from items
    left join prices on (
        items.id = prices.item_id 
        and prices.price = (
            select min(price)
            from prices
            where item_id = items.id
        )
    );
    

提交回复
热议问题