Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

bring certain values to top 1

Status
Not open for further replies.

thompom

Technical User
Dec 4, 2006
395
GB
hi this might not be an sql question but here goes

the folowing sql groups all makes in the table and counts the total of each make

Code:
SELECT count(stock.make) AS makecount, stock.make AS makeid, stock.make AS makequick
FROM stock
GROUP BY makeid

is there a way to get certain makes at the top of the recordset - these are vw and hyundai?

i dont want to put a special orderby column in the table
because this will be a regular update from another company who produces the table and would mean adding the order column every time the table is updated.
 
Yes, use the CASE function.

Something like this should sort vw and hyundai to the top and the rest in alphabetical order:
Code:
SELECT count(stock.make) AS makecount, stock.make AS makeid, stock.make AS makequick,
CASE stock.make 
  WHEN 'vw' THEN 1 
  WHEN 'hyundai' THEN 2 
ELSE 3 
END AS id

FROM stock
GROUP BY makeid
ORDER BY id, makeid

Andrew
Hampshire, UK
 
Data in MySQL tables, like the data in the tables of any relational database, has no intrinsic ordering. The only order placed on the data is when it is being fetched from the database.



Want the best answers? Ask the best questions! TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top