MySQL Sorting by turns query

こ雲淡風輕ζ 提交于 2019-12-25 07:38:19

问题


I want to sort the following data items in the order they are presented below :

enter image description here

id      msisdn          locker     is_selected  classification  nomor
----------------------------------------------------------------------
1282    62811409xxx    Kendari         0             1           01
1138    62811409xxx    Kendari         0             1           01
3264    62811556xxx    Samarinda       0             1           02
3323    62811562xxx    Pontianak       0             1           02
3450    62811481xxx    Jayapura        0             1           03
3435    62811481xxx    Jayapura        0             1           03

Query :

(SELECT *, CONCAT(is_selected, 1) AS nomor FROM reference_number WHERE is_selected = 0 AND classification = 1 AND
LEFT(msisdn, 7) IN (6281140, 6281141, 6281142, 6281143, 6281144, 6281145, 6281146) ORDER BY RAND() LIMIT 2) UNION

(SELECT *, CONCAT(is_selected, 2) AS nomor FROM reference_number WHERE is_selected = 0 AND classification = 1 AND
LEFT(msisdn, 7) IN (6281150, 6281151, 6281152, 6281153, 6281154, 6281155, 6281156, 6281157, 6281158, 6281159) 
ORDER BY RAND() LIMIT 2) UNION

(SELECT *, CONCAT(is_selected, 3) AS nomor FROM reference_number WHERE is_selected = 0 AND classification = 1 
AND LEFT(msisdn, 7) IN (6281147, 6281148, 6281149) 
ORDER BY RAND() LIMIT 2)

ORDER BY nomor

And i want the output is :

nomor
-----
01
02
03
01
02
03
01
02
03

Any tricks to make it sort more properly?

Many Thanks!

来源:https://stackoverflow.com/questions/40061173/mysql-sorting-by-turns-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!