如何根据select语句排序行

时间:2018-04-18 02:24:45

标签: mysql

所以我有疑问:

SELECT t1.username, t1.staff_id, t1.longname, t2.*, t3.longname as appraisor_name, t4.dep, t2.staff_id as check_id, t5.deptname FROM users t1
    LEFT JOIN(
        SELECT * FROM quartz_quarter2 WHERE staff_id = '1001128' AND date = '2017'
        UNION
        SELECT * FROM quartz_quarter3 WHERE staff_id = '1001128' AND date = '2017'
        UNION             
        SELECT * FROM quartz_quarter4 WHERE staff_id = '1001128' AND date = '2017'
        UNION             
        SELECT * FROM quartz_quarter1 WHERE staff_id = '1001128' AND date = '2018'            
    )t2 ON t1.staff_id = t2.staff_id
    LEFT JOIN( SELECT * FROM users )t3 ON t2.appraisor_id = t3.staff_id
    LEFT JOIN( SELECT * FROM quartz_department )t4 ON t2.role = t4.role
    LEFT JOIN( SELECT * FROM ref_department )t5 ON t1.fkdeptid = t5.deptid
    WHERE t1.staff_id = '1001128'

我希望像我选择的那样以完全相同的顺序获取数据,因此它应该是:

| username | staff_id | quarter |
+++++++++++++++++++++++++++++++++
| YEVGENIY |  1001128 |    2    |
| YEVGENIY |  1001128 |    3    |
| YEVGENIY |  1001128 |    4    |
| YEVGENIY |  1001128 |    1    |

但我得到的是:

enter image description here

有没有办法根据select语句订购?

1 个答案:

答案 0 :(得分:1)

您可以使用FIND_IN_SET()功能。

ORDER BY FIND_IN_SET(quarter, '2,3,4,1);

此函数将返回指定集合中值的位置。