[SOLVED] How to Order by a specific column name in my SQL

Issue

I’m on a work of a social network site, so I want to list the users based on logged in user location

My table sample table structure is (id, name, username, password, country, state)

Id, name, username, password, country, state

1, rameez, rameezrami, password, india, kerala

2, rameez1, rameezrami, password, india, kerala

3, rameez2, rameezrami, password, country1, state1

4, rameez3, rameezrami, password, country2, state2

If logged in user is from kerala I want to list all kerala users first then all other state users from india, then from other country how can I do this?

Solution

I think this will help you.

SELECT * FROM tableName WHERE (state='kerala' OR country='India' OR 1=1) ORDER BY state='kerala' DESC, country='India' DESC

Answered By – Tushar Bhaware

Answer Checked By – Mary Flores (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *