繁体   English   中英

sql SORT BY 对 UNION 查询

[英]sql SORT BY on a UNION query

实际上我尝试对我的 sql 进行排序,但它不是排序

我需要添加 SORT BY TSTATUS

 $sql="
(
    SELECT
        tt.tstatus,
        tt.ticketnbr,
        tt.col1,
        tt.col2,
        NULL as col3,
        tt.col4,
        tt.col5,
        tt.col6,
        tt.col7,
        tt.col8,
        'cmg' as tickettype,
        CASE WHEN cl.parentid IS NOT NULL THEN 1 ELSE 0 END as has_log
    FROM
        aradmin.cmg_troubleticket tt
        LEFT JOIN ARADMIN.TT_CUSTOMERLOGENTRY cl 
            ON ( tt.ticketnbr=cl.parentid AND cl.schema='AR:TroubleTicket' AND cl.status=0 )
    WHERE
      ( tt.TSTATUS < 9 )
      {$customer_list}             
) UNION (
    SELECT
        tt.tstatus,
        tt.ticketnbr,
        tt.col1,
        tt.col2,
        tt.col3,
        tt.col4,
        tt.col5,
        tt.col6,
        tt.col7,
        tt.col8,
        'ar' as tickettype,
        CASE WHEN cl.parentid IS NOT NULL THEN 1 ELSE 0 END as has_log
    FROM
        aradmin.ar_troubleticket tt
        LEFT JOIN ARADMIN.TT_CUSTOMERLOGENTRY cl 
            ON ( tt.ticketnbr=cl.parentid AND cl.schema='AR:TroubleTicket' AND cl.status=0 )
    WHERE
        ( tt.TSTATUS < 10 )
        {$customer_list} 
)";

只需在查询末尾附加ORDER BY tt.tstatus即可获得排序结果。

创建 UNION 结果的虚拟表并按您需要的属性排序。 尝试这个 :

SELECT * FROM (
              SELECT
                tt.tstatus,
                tt.ticketnbr,
                tt.col1,
                tt.col2,
                NULL as col3,
                tt.col4,
                tt.col5,
                tt.col6,
                tt.col7,
                tt.col8,
                'cmg' as tickettype,
              CASE WHEN cl.parentid IS NOT NULL THEN 1 ELSE 0 END as has_log
              FROM
              aradmin.cmg_troubleticket tt
                LEFT JOIN
                  ARADMIN.TT_CUSTOMERLOGENTRY cl 
            ON ( tt.ticketnbr=cl.parentid AND cl.schema='AR:TroubleTicket' AND cl.status=0 )
        WHERE
          ( tt.TSTATUS < 9 )
          {$customer_list} 

      ) UNION (
        SELECT
          tt.tstatus,
          tt.ticketnbr,
          tt.col1,
          tt.col2,
          tt.col3,
                tt.col4,
                tt.col5,
                tt.col6,
                tt.col7,
                tt.col8,
                'ar' as tickettype,
                CASE WHEN cl.parentid IS NOT NULL THEN 1 ELSE 0 END as has_log
              FROM
                aradmin.ar_troubleticket tt
                LEFT JOIN
                  ARADMIN.TT_CUSTOMERLOGENTRY cl 
                  ON ( tt.ticketnbr=cl.parentid AND cl.schema='AR:TroubleTicket' AND cl.status=0 )
              WHERE
                ( tt.TSTATUS < 10 )
                {$customer_list} 
          )) dummy ORDER BY dummy.tstatus 

您只需要在查询的最后添加ORDER BY子句。 在标准 SQL 中, ORDER BY应用于整个结果集。

此外,我相信您不需要UNION ed 子查询周围的括号,我删除了它们。

询问 :

SELECT
    tt.tstatus,
    tt.ticketnbr,
    tt.col1,
    tt.col2,
    NULL as col3,
    tt.col4,
    tt.col5,
    tt.col6,
    tt.col7,
    tt.col8,
    'cmg' as tickettype,
    CASE WHEN cl.parentid IS NOT NULL THEN 1 ELSE 0 END as has_log
FROM
    aradmin.cmg_troubleticket tt
    LEFT JOIN ARADMIN.TT_CUSTOMERLOGENTRY cl 
        ON ( tt.ticketnbr=cl.parentid AND cl.schema='AR:TroubleTicket' AND cl.status=0 )
WHERE
  ( tt.TSTATUS < 9 )
  {$customer_list}
UNION
SELECT
    tt.tstatus,
    tt.ticketnbr,
    tt.col1,
    tt.col2,
    tt.col3,
    tt.col4,
    tt.col5,
    tt.col6,
    tt.col7,
    tt.col8,
    'ar' as tickettype,
    CASE WHEN cl.parentid IS NOT NULL THEN 1 ELSE 0 END as has_log
FROM
    aradmin.ar_troubleticket tt
    LEFT JOIN ARADMIN.TT_CUSTOMERLOGENTRY cl 
        ON ( tt.ticketnbr=cl.parentid AND cl.schema='AR:TroubleTicket' AND cl.status=0 )
WHERE
   ( tt.TSTATUS < 10 )
   {$customer_list}           
ORDER BY tstatus

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM