简体   繁体   中英

SQL ORDER BY IN SELECT FROM STRING SPLIT or Comma Separated Value

I would like to know if is it possible in SQL to order by the query from the String_split or csv like this

select item_number 
from table1 
order by ('335484,105666,105681,338706,144516') 
      offset 0 rows fetch next 30 rows only

This list is the item_number with offset and fetch next ..

Thanks

不,这是不可能的,您尝试订购的每个字段都应该分开,在您的情况下,每个字段都在单引号内,然后用逗号分隔

If I understand correctly, you can use string operations for this. Because the question is tagged linq, let me assume you are using SQL Server:

select item_number 
from table1 
order by charindex(',', item_number + ',', ',' + '335484,105666,105681,338706,144516' + ',') 
offset 0 rows fetch next 30 rows only;

This returns the location of the number in the string and uses that location for ordering.

You can do something similar in other databases.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM