2

I have a table with a column which can contain: "kldr", "bgg" or an integer.

Now i want a query with the result in the following order:

 1. kldr 
 2. kldr 
 3. bgg 
 4. bgg 
 5. 1  
 6. 2 
 7. 3
 8. etc.

Can anyone help me out? My current query is obvious not working.

SELECT * 
FROM table 
WHERE column_x='$value_x' 
ORDER BY column_y LIKE '%kldr%' ASC, LIKE '%bgg%' ASC, floor ASC

1 Answer 1

5

You can use a CASE expression in the ORDER BY clause:

SELECT * 
FROM table 
WHERE column_x='$value_x' 
ORDER BY CASE WHEN column_y LIKE '%kldr%' THEN 1
              WHEN column_y LIKE '%bgg%' THEN 2
              ELSE column_y + 2
         END
Sign up to request clarification or add additional context in comments.

2 Comments

Ah thanks. 'THEN' sets only the order position as i understand?
@RamonBakker THEN determines the number returned by the CASE expression.

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.