MySQL Forums
Forum List  »  Newbie

Re: select min record from many to many relationship
Posted by: Peter Brawley
Date: June 21, 2018 11:30AM

Your problem is the "Within-group aggregates" problem, see discussion under that heading at https://www.artfulsoftware.com/infotree/queries.php.

Your best bet might be a variant of #5 in the above list. It's not clear from your query whether you want the lowest minimumordrprice or the lowest price; assuming the former, you query might be ...

select p.name, s.name, s.minimumorderprice, h.* 
from product              as p
join product_has_supplier as h on p.product_id = h.product_product_id
join supplier             as s on h.supplier_supplier_id = s.supplier_id
where p.product_id = @productid and h.isparallel = @isparallel 
order by s.minimumorderprice asc limit 1;

Comma join syntax has limitations. Explicit join syntax is easier to write debug & maintain.

Limit N to see the N best N prices.

Options: ReplyQuote




Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.