代碼如下:
Select *
FROM table1
Where (ID IN (3,5,1,4,2))
代碼如下:
Select *
FROM table1
Where (ID IN (3,5,1,4,2))如果In後面的條件都是數字,那MSSQL排序應該為
代碼如下:
Select *
FROM table1
Where (ID IN (3,5,1,4,2))
orDER BY CHARINDEX(',' + CONVERT(nvarchar, ID) + ',', ',' + CONVERT(nvarchar, Replace('3,5,1,4,2', ' ','')) + ',')
代碼如下:
Select *
FROM table1
Where (ID IN (3,5,1,4,2))
orDER BY CHARINDEX(',' + CONVERT(nvarchar, ID) + ',', ',' + CONVERT(nvarchar, Replace('3,5,1,4,2', ' ','')) + ',')改進方法,不去處理空格,直接改用空格判斷.可以用來判斷少數有空格的字符條件.
代碼如下:
Select *
FROM table1
Where (ID IN (3,5,1,4,2))
orDER BY PATINDEX('% ' + CONVERT(nvarchar(4000), ID) + ' %', ' ' + CONVERT(nvarchar(4000), Replace('3,5,1,4,2', ',',' , ')) + ' ')
代碼如下:
Select *
FROM table1
Where (ID IN (3,5,1,4,2))
orDER BY PATINDEX('% ' + CONVERT(nvarchar(4000), ID) + ' %', ' ' + CONVERT(nvarchar(4000), Replace('3,5,1,4,2', ',',' , ')) + ' ')
其實還是在SQL外處理好條件字符串再進行查詢和排序比較好.
對於MYSQL排序可能要改為(MYSQL不熟):
代碼如下:
Select *
FROM table1
Where (ID IN (3,5,1,4,2))
orDER BY FIND_IN_SET(ID, '3,5,1,4,2')