如何根据select语句对行进行排序

y3bcpkx1  于 2021-06-25  发布在  Mysql
关注(0)|答案(1)|浏览(278)

所以我有疑问:

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    |

但我得到的是:

有没有办法根据select语句来排序?

pcww981p

pcww981p1#

你可以用这个 FIND_IN_SET() 功能。

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

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

相关问题