MySQL Group by both groups sorted but first group 1 after group 2

I need help as is this query now Promos appear first desired but Not sorted by price [price asc] although after promos further down the non promos are sorted… This i want is both SORTED but ALL promos appear first… I have to apply “group by” but how … to group by promos and not ? promos and main cars tables shown below…well?

$ttQuery = "SELECT

             c.*,  DATE_FORMAT(c.SYS_CREATION_DATE,'%d-%b-%y') AS creationDate, p.id as promoID
           FROM 
  		 cars c
  		   
  		 LEFT OUTER JOIN promo p 
  		 
  		 ON c.ID = p.ID 
  		   				 
  		 $searchCondition
  		 
  		 ORDER BY 
  		   p.id desc, price asc, c.SYS_CREATION_DATE DESC, c.model ASC
  		 LIMIT $limit1, $limit2";

promo table

cars table …

you do not want GROUP BY, that is not what GROUP BY is for

try this –

ORDER BY CASE WHEN p.id IS NULL THEN 'humpty' ELSE 'dumpty' END , c.id DESC , price ASC , c.SYS_CREATION_DATE DESC , c.model ASC
note because it’s a LEFT OUTER JOIN, you want c.id in the ORDER BY, not p.id

1 Like

I put p.id to get first promos… you mean only in thus new query you provided. …? in this case I think better c.id desc ignore it completely. … well? To Test soon…

AT LAST SOLVED WITHOUT
, c.id DESC

thanks for your help…

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.