2017-05-16 14 views
0

レポートを生成するSQLクエリがあります。我々はsucusesfully正しい項目で整数値を更新するために私たちの設定テーブルに参加しました、我々はまた、3つのタイプのユーザーを表示するためにユニオンを使用しています。LEFT JOIN、INNER JOIN AND UNIONS、注文方法

このクエリを受け取り、それを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 

あなたはそれが結果セット全体に適用するクエリの最後にORDER BY句を追加する場合は、あなたに

答えて

1

ありがとうございます。

ORDER BY `Job Number` 

また、最初のもの以外のユニオンクエリでエイリアスを指定する必要はありません。エイリアスの最初のセットはスティックです。

+0

ありがとうございました!私たちは 'バッククォートの代わりに' –