简体   繁体   中英

MySQL sort order by array value of selected table

With eg

ORDER BY field(id, 9, 1, 4)

I can order by 9,1,4.

Is it also possible to order by a comma separated string of the same table I am selecting?

I tried

ORDER BY field(id, aFieldFromTheTableIAmSelecting)

where aFieldFromTheTableIAmSelecting is "9,1,4"

But that does not work.

This question is similiar to MySQL sort order by array value

您可以尝试find_in_set()

ORDER BY find_in_set(id, aFieldFromTheTableIAmSelecting)

You can use locate() :

ORDER BY locate(concat(',', id, ','), concat(',', aFieldFromTheTableIAmSelecting, ','))

there must be no spaces inside aFieldFromTheTableIAmSelecting .

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