1

I have some records like this

id name sequence ------------------------ 1 steve 3 2 lee 2 3 lisa 1 4 john 0 5 smith 0 

I want to display records like following

id name ------------ 1 lisa 2 lee 3 steve 4 john 5 smith 

When i am using order by clause then it display like

name ---- john smith lisa lee steve 

Query

SELECT name from tbl1 where 1 ORDER BY sequence ASC 

2 Answers 2

5
SELECT name FROM tbl1 ORDER BY sequence = 0, sequence ASC 

or

SELECT name FROM tbl1 ORDER BY case when sequence <> 0 then 1 else 2 end, sequence ASC 
Sign up to request clarification or add additional context in comments.

Comments

0

You can use query with if condition in ORDER BY clause

SELECT name from tbl1 ORDER BY IF(sequence = 0,name,sequence) ASC 

Fiddle

Output

| NAME | |-------| | lisa | | lee | | steve | | john | | smith | 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.