0

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 :)

9
  • you could substr (D) in the query. Commented Aug 19, 2014 at 9:15
  • Can you provide your table definition? Commented Aug 19, 2014 at 9:17
  • 1
    Why do you put use a prefix to distinguish drinks from food? Why don't you use another column in the table, call is is_drink? Commented Aug 19, 2014 at 9:21
  • 1
    @Austin and I seem to have interpreted your question differently. Could you show the results you want to get? Commented Aug 19, 2014 at 9:22
  • 2
    Hungarian notation in relational data. Awful. Commented Aug 19, 2014 at 9:27

2 Answers 2

3
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
Sign up to request clarification or add additional context in comments.

Comments

0

Try the below query, will work..

***select * from orders order by case

when pname LIKE "(D)%" then 1

else 2

end***

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.