How to sort ID due to where clause used with IN(..)

SELECT *
FROM `question`
WHERE `id`
IN ( 9, 11, 175, 16 )

The results will be displayed where ids are 9, 11, 175, 16, but the results will be asc by id(9,11,16,175). If you don’t want to use automatic sorting or just list according to 9,11,175,16 add ORDER BY FIELD.

SELECT *
FROM `question`
WHERE `id`
IN ( 9, 11, 175, 16 )
ORDER BY FIELD( id, 9, 11, 175, 16)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s