5

We have such table in MySQL: id - int; title - varchar; hd - tinyint; source - tinyint; active - tinyint;

How do i get data from database with such sorting:

1. hd >= 3 AND source <> 5
2. hd >= 3 AND source = 5
3. hd = 2
4. other, i.e. hd < 2

Please show me how to do it properly and one sql query?

Thank you.

2 Answers 2

6
select * from your_table
order by case when hd >= 3 AND source <> 5 then 1
              when hd >= 3 AND source = 5 then 2
              when hd = 2 then 3
              else 4
         end
Sign up to request clarification or add additional context in comments.

Comments

4

Try this:

select * 
from table_name
order by case when hd >= 3 AND source <> 5 then 1
              when hd >= 3 AND source = 5 then 2
              when hd = 2 then 3
              else 4
         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.