2014-03-06 78 views
0

訂購我有一個要求,這樣的查詢:如何通過特定組SQL

顯示在後續作業的順序排列的所有細節員工:董事,監事,ENGINEER和祕書。

僱員表結構是這樣的:

EMPLOYEE_ID,名字,姓氏,JOB_ID)

作業片劑結構是這樣的:

JOB_ID,JOB_TITLE, Min_Salary,Max_Salary)

如何按特定組進行分組?

非常感謝。

+0

你並不需要組 - 你需要訂購。 – nhgrif

回答

0
select case 
     when job_title = 'DIRECTOR' then 
      1 
     when job_title = 'SUPERVISOR' then 
      2 
     when job_title = 'ENGINEER' then 
      3 
     when job_title = 'CLERK' then 
      4 
     else 
      0 
     end as seq, 
     employees.*, 
     jobs.* 
    from employees 
    join jobs 
    on employees.job_id = jobs.job_id 
order by 1 
0

如果你正在使用MySQL(所暗示的「mysqli的」標籤),然後使用字段功能:

SELECT e.* 
FROM Employee e INNER JOIN 
    Job J 
    ON J.Job_ID = E.Job_ID 
ORDER BY field(J.Job_Title, 'DIRECTOR', 'SUPERVISOR', 'ENGINEER', 'CLERK'); 
1
select 
     * 
    from employees employees 
    join jobs jobs 
    on employees.job_id = jobs.job_id 
order by case 
     when job_title = 'DIRECTOR' then 
      1 
     when job_title = 'SUPERVISOR' then 
      2 
     when job_title = 'ENGINEER' then 
      3 
     when job_title = 'CLERK' then 
      4 
     else 
      0 end