2017-05-16 95 views
0

我們有一個產生報告的SQL查詢。我們合理地加入了我們的設置表來更新正確的項目的整數值,我們還使用聯合來顯示我們的三種類型的用戶。LEFT JOIN,INNER JOIN和工會,如何ORDER BY

我們希望通過e_job.objId場藉此查詢和訂購它,但是我們發現與多個連接與聯合它真的很難。

如果有人可以幫助我們將不勝感激。

SELECT e_job.objId AS 'Job Number', e_student.Lastname AS 'Last Name', e_student.Name AS 'First Name', e_asset.objId AS 'Asset Number', e_asset.aSerialNumber AS 'Serial Number', si.sLabel AS 'Issue', srt.sLabel AS 'Repair Type', ss.sLabel As 'Status', e_job.note 'Description',FROM_UNIXTIME(e_job.jCreatedAt,'%d/%m/%y') AS 'Date Created' 
       FROM e_job 
       INNER JOIN e_student ON e_job.jName = e_student.username 
       INNER JOIN e_asset ON e_job.jAsset = e_asset.aId 
       LEFT JOIN e_settings si ON si.sKey = 'job_issue' AND si.sValue = e_job.jIssue 
       LEFT JOIN e_settings srt ON srt.sKey = 'job_rep_type' AND srt.sValue = e_job.jRepairType 
       LEFT JOIN e_settings ss ON ss.sKey = 'job_status' AND ss.sValue = e_job.jStatus 
       WHERE jStatus = 1 && jRepairType = 2 
       UNION 
       SELECT e_job.objId AS 'Job Number', e_teachers.Lastname AS 'Last Name', e_teachers.Name AS 'First Name', e_asset.objId AS 'Asset Number', e_asset.aSerialNumber AS 'Serial Number', si.sLabel AS 'Issue', srt.sLabel AS 'Repair Type', ss.sLabel As 'Status', e_job.note 'Description',FROM_UNIXTIME(e_job.jCreatedAt,'%d/%m/%y') AS 'Date Created' 
       FROM e_job 
       INNER JOIN e_teachers ON e_job.jName = e_teachers.username 
       INNER JOIN e_asset ON e_job.jAsset = e_asset.aId 
       LEFT JOIN e_settings si ON si.sKey = 'job_issue' AND si.sValue = e_job.jIssue 
       LEFT JOIN e_settings srt ON srt.sKey = 'job_rep_type' AND srt.sValue = e_job.jRepairType 
       LEFT JOIN e_settings ss ON ss.sKey = 'job_status' AND ss.sValue = e_job.jStatus 
       WHERE jStatus = 1 && jRepairType = 2 
       UNION 
       SELECT e_job.objId AS 'Job Number', e_supportStaff.Lastname AS 'Last Name', e_supportStaff.Name AS 'First Name', e_asset.objId AS 'Asset Number', e_asset.aSerialNumber AS 'Serial Number', si.sLabel AS 'Issue', srt.sLabel AS 'Repair Type', ss.sLabel As 'Status', e_job.note 'Description',FROM_UNIXTIME(e_job.jCreatedAt,'%d/%m/%y') AS 'Date Created' 
       FROM e_job 
       INNER JOIN e_supportStaff ON e_job.jName = e_supportStaff.username 
       INNER JOIN e_asset ON e_job.jAsset = e_asset.aId 
       LEFT JOIN e_settings si ON si.sKey = 'job_issue' AND si.sValue = e_job.jIssue 
       LEFT JOIN e_settings srt ON srt.sKey = 'job_rep_type' AND srt.sValue = e_job.jRepairType 
       LEFT JOIN e_settings ss ON ss.sKey = 'job_status' AND ss.sValue = e_job.jStatus 
       WHERE jStatus = 1 && jRepairType = 2 

謝謝

回答

1

如果添加ORDER BY子句應該適用於整個查詢結果集的結尾。您可以嘗試添加此:

ORDER BY `Job Number` 

而且,你不需要在比第一個其他聯合查詢指定別名。第一組別名是什麼棒。

+0

謝謝!我們使用'而不是反引號' –