quatchis
quatchis

Reputation: 63

Using PHP/MYSQL to sort data based on partial values

I got prefix values setup for my products that help me identify drinks from food. If its a drink it will contain (D) as the prefix.

(D)Beer
Burger
(D)Soft Drink
Sandwich

SELECT * FROM orders  order by pname 

I want to sort the drinks together and the food together in a single query. Hope that makes sence and thx for the help :)

Upvotes: 0

Views: 63

Answers (2)

Hakim Saifee
Hakim Saifee

Reputation: 71

Try the below query, will work..

***select * from orders order by case

when pname LIKE "(D)%" then 1

else 2

end***

Upvotes: 0

Barmar
Barmar

Reputation: 781721

SELECT *
FROM orders
ORDER BY LEFT(pname, 3) = "(D)", pname

LEFT(pname, 3) = "(D)" will be 0 for drinks, 1 for foods. So this will put all drinks first, then all foods. So the results will be:

(D)Beer
(D)Soft Drink
Burger
Sandwich

Upvotes: 3

Related Questions