Read rows from database follow a specific row in mysql

Sometimes you need to show a specific row first then follow remaining rows in mysql database. For example, I want to show list of countries but a country name like Australia on top then other countries.
I am going to show you.

Suppose mysql table is "country" and query used to retrieve country information is :

 "SELECT * FROM country ORDER BY country_id ASC"

Output:

Read rows from database follow a specific row in mysql - 1


If you want to show Australia on top of the row. Use following script

SELECT *, IF(country_code = 'AU', 1, 0) AS uses_default
FROM country
ORDER BY uses_default DESC, country_id ASC


Read rows from database follow a specific row in mysql - 2

I am taking example of Australia, But you can put any country name on top. If you have any question, you can comment here. I will try to give you response as soon as possible.






0 Comments
Disqus
Fb Comments
Comments :

0 comments:

Post a Comment