Mysql

我嘗試將 select union 與 order by 一起使用,現在我可以選擇它,但似乎它不像我期望的那樣按日期排序,為什麼?

  • July 15, 2016
SELECT *
FROM (
   SELECT sale.NAME AS "Name"
       ,sale.responsible AS "Responsible:data:200"
       ,sale.action_type AS "Action Type:data:200"
       ,sale.id_customer AS "Id_Customer"
       ,sale.workflow_state AS "Workflow:data:200"
       ,sale.close_date AS "Close Date:data:200"
       ,sale.modified_by AS "Done By:data:200"
   FROM `tabsales_action` sale
   WHERE sale.workflow_state = 'Done'
   ORDER BY sale.close_date DESC
   ) `tabsales_action`

UNION

SELECT *
FROM (
   SELECT mfi.NAME AS "Name"
       ,mfi.responsible AS "Responsible:data:200"
       ,mfi.action_type AS "Action Type:data:200"
       ,mfi.id_customer AS "Id_Customer"
       ,mfi.workflow_state AS "Workflow:data:200"
       ,mfi.close_date AS "Close Date:data:200"
       ,mfi.modified_by AS "Done By:data:200"
   FROM `tabmfi_loan_action` mfi
   WHERE mfi.workflow_state = 'Done'
   ORDER BY mfi.close_date DESC
   ) `tabmfi_loan_action`

UNION

SELECT *
FROM (
   SELECT credit.NAME AS "Name"
       ,credit.responsible AS "Responsible:data:200"
       ,credit.action_type AS "Action Type:data:200"
       ,credit.id_customer AS "Id_Customer"
       ,credit.workflow_state AS "Workflow:data:200"
       ,credit.close_date AS "Close Date:data:200"
       ,credit.modified_by AS "Done By:data:200"
   FROM `tabin_house_credit_action` credit
   WHERE credit.workflow_state = 'Done'
   ORDER BY credit.close_date DESC
   ) `tabin_house_credit_action`

UNION

SELECT *
FROM (
   SELECT service.NAME AS "Name"
       ,service.responsible AS "Responsible:data:200"
       ,service.action_type AS "Action Type:data:200"
       ,service.id_customer AS "Id_Customer"
       ,service.workflow_state AS "Workflow:data:200"
       ,service.close_date AS "Close Date:data:200"
       ,service.modified_by AS "Done By:data:200"
   FROM `tabservices_planning` service
   WHERE service.workflow_state = 'Done'
   ORDER BY service.close_date DESC
   ) `tabservices_planning`

UNION

SELECT *
FROM (
   SELECT account.NAME AS "Name"
       ,account.responsible AS "Responsible:data:200"
       ,account.action_type AS "Action Type:data:200"
       ,account.id_customer AS "Id_Customer"
       ,account.workflow_state AS "Workflow:data:200"
       ,account.close_date AS "Close Date:data:200"
       ,account.modified_by AS "Done By:data:200"
   FROM `tabaccounting_action` account
   WHERE account.workflow_state = 'Done'
   ORDER BY account.close_date DESC
   ) `tabaccounting_action`

看起來您正在按順序在所有表中按列close_date排序。代替這種方法,所有表格,在最終結果中,您可以使用as 。DESC``UNION``SELECT UNION``ORDER BY``Close Date:data:200``DESC

工作查詢將是:

SELECT T.*
FROM (
   SELECT sale.NAME AS "Name"
       ,sale.responsible AS "Responsible:data:200"
       ,sale.action_type AS "Action Type:data:200"
       ,sale.id_customer AS "Id_Customer"
       ,sale.workflow_state AS "Workflow:data:200"
       ,sale.close_date AS "Close Date:data:200"
       ,sale.modified_by AS "Done By:data:200"
   FROM `tabsales_action` sale
   WHERE sale.workflow_state = 'Done'

   UNION

   SELECT mfi.NAME AS "Name"
       ,mfi.responsible AS "Responsible:data:200"
       ,mfi.action_type AS "Action Type:data:200"
       ,mfi.id_customer AS "Id_Customer"
       ,mfi.workflow_state AS "Workflow:data:200"
       ,mfi.close_date AS "Close Date:data:200"
       ,mfi.modified_by AS "Done By:data:200"
   FROM `tabmfi_loan_action` mfi
   WHERE mfi.workflow_state = 'Done'

   UNION

   SELECT credit.NAME AS "Name"
       ,credit.responsible AS "Responsible:data:200"
       ,credit.action_type AS "Action Type:data:200"
       ,credit.id_customer AS "Id_Customer"
       ,credit.workflow_state AS "Workflow:data:200"
       ,credit.close_date AS "Close Date:data:200"
       ,credit.modified_by AS "Done By:data:200"
   FROM `tabin_house_credit_action` credit
   WHERE credit.workflow_state = 'Done'

   UNION

   SELECT service.NAME AS "Name"
       ,service.responsible AS "Responsible:data:200"
       ,service.action_type AS "Action Type:data:200"
       ,service.id_customer AS "Id_Customer"
       ,service.workflow_state AS "Workflow:data:200"
       ,service.close_date AS "Close Date:data:200"
       ,service.modified_by AS "Done By:data:200"
   FROM `tabservices_planning` service
   WHERE service.workflow_state = 'Done'

   UNION

   SELECT account.NAME AS "Name"
       ,account.responsible AS "Responsible:data:200"
       ,account.action_type AS "Action Type:data:200"
       ,account.id_customer AS "Id_Customer"
       ,account.workflow_state AS "Workflow:data:200"
       ,account.close_date AS "Close Date:data:200"
       ,account.modified_by AS "Done By:data:200"
   FROM `tabaccounting_action` account
   WHERE account.workflow_state = 'Done'
) AS T
ORDER BY T.`Close Date:data:200` DESC

引用自:https://dba.stackexchange.com/questions/143584