按所有列排序

时间:2015-12-27 16:05:19

标签: mysql sql

我只是想通过他们的部分和位置来排列候选人名单。我使用OR Clause但是它工作,不支持许多列。这是我的代码:请帮帮我。感谢

SELECT student.firstname, 
       candidates.partylist, 
       candidates.position 
FROM   student 
       INNER JOIN candidates 
               ON student.student_id = candidates.student_id 
ORDER  BY partylist, 
          position = 'President' 
           OR position = 'VicePresident' 
           OR position = 'Secretary' 
           OR position = 'Treasurer' 
           OR position = 'Auditor'; 

2 个答案:

答案 0 :(得分:4)

改为使用FIELD

SELECT student.firstname, candidates.partylist, candidates.position 
FROM student 
INNER JOIN candidates ON student.student_id = candidates.student_id 
ORDER BY partylist, 
         FIELD(position, 'President', 'VicePresident', 'Secretary', 
               'Treasurer', 'Auditor');

答案 1 :(得分:1)

我想您需要在Case而不是Order by条件中OR

尝试这样的事情

SELECT student.firstname, 
       candidates.partylist, 
       candidates.position 
FROM   student 
       INNER JOIN candidates 
               ON student.student_id = candidates.student_id 
ORDER  BY partylist, 
          CASE position 
            WHEN 'President' THEN 1 
            WHEN 'VicePresident' THEN 2 
            WHEN 'Secretary' THEN 3 
            WHEN 'Treasurer' THEN 4 
            WHEN 'Auditor' THEN 5 
            ELSE 6  -- Here replace 6 with 0
          END 

注意:如果您希望Order by中的提及列表以外的数据先出现,请将6替换为0

相关问题