Order BY common usage I'm not going to mention it.
The needs of the project are changeable
Let's take a look at a few weird sort requests
--Create a table first
CREATE TABLE AI (
ID int NOT NULL,
No varchar (TEN) NOT NULL
)
Go
--insert data into the table
INSERT INTO AI
Select 105, ' 2 '
UNION ALL
Select 105, ' 1 '
UNION ALL
Select 103, ' 1 '
UNION ALL
Select 105, ' 4 '
Go
--The query effect is as follows:
SELECT * from AI
Go
ID no
----------- ----------
105 2
105 1
103 1
105 4
I.
--The required query results are as follows
-that is, the data for no column is ordered by ' 4 ', ' 1 ', ' 2 '
ID no
----------- ----------
105 4
105 1
103 1
105 2
--Solution 1
--Using function charindex
SELECT * from AI
ORDER BY CHARINDEX (No, ' 4,1,2 ')
--Solution 2, and each group is sorted in descending order of IDs
--Using Function case
SELECT * from AI
no= ' 4 ' then 1
When no= ' 1 ' then 2
When no= ' 2 ' then 3
End,id desc