1

I have a doctors table where i have many columns, like id, name, email, etc. and two of them is is_featured and is_top.

These both column type is ENUM having value either 'y' or 'n'.

I want to get the doctors first having is_features='y' and then those doctors having is_top='y' and last is_featured='n' and is_top='n'

so basically i want the order of the doctors like these:

  1. is_featured='y'
  2. is_top='y'
  3. is_featured='n'
  4. is_top='n'

Can any one help how can i achieve this?

I am totally unaware about how to syntax these type of sql query?

1
  • It is better to use TINYINT(1) with values 1 and 0 for TRUE and FALSE, respectively, so the value gets casted to boolean correctly. Commented Feb 21, 2018 at 10:17

1 Answer 1

1

There is a shortcut form for that in mysql:

SELECT ...
FROM ...
ORDER BY    is_featured = 'y' DESC,
            is_top = 'y' DESC,
            is_featured = 'n' DESC,
            is_top = 'n' DESC

Otherwise, if you want to make it standard which will run in other RDBMS as well, use CASE

SELECT ...
FROM ...
ORDER BY    CASE WHEN is_featured = 'y' THEN 1 ELSE 0 END DESC,
            CASE WHEN is_top='y' THEN 1 ELSE 0 END DESC,
            CASE WHEN is_featured='n' THEN 1 ELSE 0 END DESC,
            CASE WHEN is_top='n' THEN 1 ELSE 0 END DESC
Sign up to request clarification or add additional context in comments.

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.