How to impose conditions on a "GROUP BY" statement - MySQL -


i have following mysql query:

select *    products  catalog = 1  group style  order name asc  limit 0, 100 

since have multiple products same "style", return information whatever first product has "style" (through group condition).

because products have "discount", question follows: how make "group style" condition gives first priority product has discount? ultimate goal use 1 query instead of after going through each returned style , checking if there products discount

if specify "group style, discount", returns 2 products same style: 1 , 1 without discount. not need -- need return 1 match (unique) style give preference items match style have discount.

you cannot impose priority within groupped set in single query. can restrict results using before applying group by, strip result of rows not satisfying condition. find order within groupped sets use query this: (assuming id primary key here)

select p.*   ( select style,max(discount) highest_discount      products     catalog = 1     group style ) p1 join products p on p.id =   ( select pu.id      products pu     pu.catalog = 1       , pu.style = p1.style       , pu.discount = p1.highest_discount     limit 1 ) 

Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -