user2536273
user2536273

Reputation: 69

Sqlite Sort by one column based from results of other query

Database Structure

S.no UserName ID OrderT
"1" "UserName" "Ram" "1"
"2" "UserName" "Rec" "2"
"3" "UserName" "Del" "3"
"4" "UserName" "Bul" "4"
"5" "UserName" "Rec" "5"

Result data to be like

"5" "UserName" "Rec" "5"
"2" "UserName" "Rec" "2"
"4" "UserName" "Bul" "4"
"3" "UserName" "Del" "3"
"1" "UserName" "Ram" "1"

--> Latest entered record should appear on Top with already present records with same ID value.

Can anyone help me out. I tried like this, which is not working as expected select * from locations where orderT DESC order by ID

Upvotes: 0

Views: 30

Answers (1)

CL.
CL.

Reputation: 180070

To sort the latest ID before all others, add another ORDER BY expression that checks for the latest ID:

SELECT *
FROM Locations
ORDER BY ID <> (SELECT ID
                FROM Locations
                ORDER BY OrderT DESC
                LIMIT 1),
         OrderT DESC;

Upvotes: 1

Related Questions