Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order by selected record in sql

Tags:

sql

mysql

I have a table like

city | pincode

abcd | 123456
xyz  | 326545
asd  | 625844
city | 999999

and I want the result sorted with the selected pincode first

If I select pincode is 625844 which city is asd it must be show first

Desired output:

    city | pincode

    asd  | 625844  <<-- this is selected pincode must be first
    abcd | 123456
    xyz  | 326545
    city | 999999
like image 639
Veer Avatar asked Feb 21 '23 06:02

Veer


1 Answers

select * from tbl 
order by (case when pincode = '625844' then 0 else 1 end), pincode

or if the selected pincode is being passed in as a parameter @pincode, this should work

select * from tbl 
order by (case when pincode = @pincode then 0 else 1 end), pincode
like image 164
John Gathogo Avatar answered Feb 27 '23 11:02

John Gathogo