【问题标题】:Mysql multiple ORDER BY with UNIONMysql 多个 ORDER BY 和 UNION
【发布时间】:2016-11-22 13:07:17
【问题描述】:

我想按日期获取数据。我正在使用联合。

我浏览了以下网址。但我没有得到明确的解决方案。

mysql order by with union doesn't seem to work

我的查询

(SELECT n.nid, 
        Max(na.gid)                 AS mid, 
        fav.field_date_posted_value AS pdate 
 FROM   `node` AS n 
 JOIN nodeaccess AS na 
   ON na.nid = n.nid 
 LEFT JOIN field_data_field_date_posted AS fav 
        ON fav.entity_id = n.nid 
 WHERE  ( na.gid IN( 10, 11 ) 
          AND ( n.status = '1' ) 
          AND ( n.type IN ( 'article', 'blog', 'events', 'media', 
                            'press_releases', 'expert_speak', 'feature', 
                            'case_study', 
                            'news', 'the_igtb_series', 'trend', 'white_paper' ) 
              ) ) 
 GROUP  BY n.nid 
 ORDER  BY pdate DESC) 

UNION 

(SELECT n.nid, 
        Max(na.gid)                 AS mid, 
        fav.field_date_posted_value AS pdate 
 FROM   `node` AS n 
 JOIN nodeaccess AS na 
   ON na.nid = n.nid 
 LEFT JOIN field_data_field_date_posted AS fav 
        ON fav.entity_id = n.nid 
 WHERE  ( na.gid IN( 2 ) 
          AND ( n.status = '1' ) 
          AND ( n.type IN ( 'article', 'blog', 'events', 'media', 
                            'press_releases', 'expert_speak', 'feature', 
                            'case_study', 
                            'news', 'the_igtb_series', 'trend', 'white_paper' ) 
              ) ) 
 GROUP  BY n.nid 
 ORDER  BY pdate DESC) 
LIMIT 
10 

我的结果

+-------+------+---------------------+
| nid   | mid  | pdate               |
+-------+------+---------------------+
| 12472 |   10 | 2015-05-11 00:00:00 |
| 12473 |   10 | 2015-04-03 00:00:00 |
| 12475 |   10 | 2015-06-08 00:00:00 |
| 12476 |   10 | 2015-12-15 01:55:48 |
| 12477 |   10 | 2014-06-30 00:00:00 |
| 12478 |   10 | 2013-12-26 00:00:00 |
| 12482 |   10 | 2014-02-02 00:00:00 |
| 12483 |   10 | 2014-09-01 00:00:00 |
| 12484 |   10 | 2015-12-04 00:00:00 |
| 12485 |   10 | 2015-08-14 00:00:00 |
+-------+------+---------------------+

在上面的 url 中,日期顺序不起作用。我不想像平常那样订购。我需要为两个选择查询单独排序(在 UNION 中使用)。

更新 2:

(SELECT n.nid, max(na.gid) as mid, fav.field_date_posted_value as pdate FROM `node` as n
JOIN nodeaccess AS na ON na.nid = n.nid
LEFT JOIN field_data_field_date_posted AS fav ON fav.entity_id = n.nid
WHERE (na.gid IN(10,11) AND (n.status = '1') AND (n.type IN  ('article','blog', 'events', 'media', 'press_releases', 'expert_speak', 'feature', 'case_study', 'news', 'the_igtb_series', 'trend', 'white_paper')) )
GROUP BY n.nid 
ORDER BY pdate DESC)

UNION

(SELECT n.nid, max(na.gid) as mid, fav.field_date_posted_value as pdate FROM `node` as n
JOIN nodeaccess AS na ON na.nid = n.nid
LEFT JOIN field_data_field_date_posted AS fav ON fav.entity_id = n.nid
WHERE (na.gid IN(2) AND (n.status = '1') AND (n.type IN  ('article','blog', 'events', 'media', 'press_releases', 'expert_speak', 'feature', 'case_study', 'news', 'the_igtb_series', 'trend', 'white_paper')) )
GROUP BY n.nid 
ORDER BY pdate DESC)

ORDER BY pdate DESC LIMIT 10

上面给出了下面的结果。

+-------+------+---------------------+
| nid   | mid  | pdate               |
+-------+------+---------------------+
| 12789 |   11 | 2016-09-26 00:00:00 |
| 12826 |    2 | 2016-07-13 00:00:00 |
| 12845 |    2 | 2016-07-05 00:00:00 |
| 12823 |   10 | 2016-06-21 00:00:00 |
| 12822 |    2 | 2016-06-17 00:00:00 |
| 12821 |   10 | 2016-06-07 00:00:00 |
| 12635 |   10 | 2016-06-07 00:00:00 |
| 12821 |    2 | 2016-06-07 00:00:00 |
| 12633 |   10 | 2016-05-25 02:19:29 |
| 12548 |   10 | 2016-05-20 00:00:00 |
+-------+------+---------------------+

在此结果中,我不想重新排序“mid”列

【问题讨论】:

  • 您可以在查询末尾添加ORDER BY pdate DESC,就在LIMIT 10之前
  • 你能告诉我们你想要的输出是什么吗?
  • @mwafi 感谢您的评论。我已经尝试过了,但没有给出我预期的结果。
  • 但是根据你的表mid没有排序

标签: mysql sql


【解决方案1】:

试试这个

SELECT n.nid, max(na.gid) as mid,fav.field_date_posted_value, UNIX_TIMESTAMP(fav.field_date_posted_value) as pdate, 1 as ob FROM `node` as n
JOIN nodeaccess AS na ON na.nid = n.nid
LEFT JOIN field_data_field_date_posted AS fav ON fav.entity_id = n.nid
WHERE (na.gid IN(10,11) AND (n.status = '1') AND (n.type IN  ('article','blog', 'events', 'media', 'press_releases', 'expert_speak', 'feature', 'case_study', 'news', 'the_igtb_series', 'trend', 'white_paper')) )
GROUP BY n.nid 


UNION ALL

SELECT n.nid, max(na.gid) as mid,fav.field_date_posted_value, UNIX_TIMESTAMP(fav.field_date_posted_value) as pdate, 2 as ob FROM `node` as n
JOIN nodeaccess AS na ON na.nid = n.nid
LEFT JOIN field_data_field_date_posted AS fav ON fav.entity_id = n.nid
WHERE (na.gid IN(2) AND (n.status = '1') AND (n.type IN  ('article','blog', 'events', 'media', 'press_releases', 'expert_speak', 'feature', 'case_study', 'news', 'the_igtb_series', 'trend', 'white_paper')) )
GROUP BY n.nid  

ORDER BY ob ASC, pdate DESC

修复了仅按日期或组的顺序

【讨论】:

  • 按预期工作。谢谢@Nandakumar
【解决方案2】:

您需要将两个 select 语句的并集结果包装起来,并按pdate 排序。

如下:

SELECT * from 
    (
        (SELECT n.nid, max(na.gid) as mid, fav.field_date_posted_value as pdate FROM `node` as n
        JOIN nodeaccess AS na ON na.nid = n.nid
        LEFT JOIN field_data_field_date_posted AS fav ON fav.entity_id = n.nid
        WHERE (na.gid IN(10,11) AND (n.status = '1') AND (n.type IN  ('article','blog', 'events', 'media', 'press_releases', 'expert_speak', 'feature', 'case_study', 'news', 'the_igtb_series', 'trend', 'white_paper')) )
        GROUP BY n.nid 
        ORDER BY pdate DESC)

        UNION

        (SELECT n.nid, max(na.gid) as mid, fav.field_date_posted_value as pdate FROM `node` as n
        JOIN nodeaccess AS na ON na.nid = n.nid
        LEFT JOIN field_data_field_date_posted AS fav ON fav.entity_id = n.nid
        WHERE (na.gid IN(2) AND (n.status = '1') AND (n.type IN  ('article','blog', 'events', 'media', 'press_releases', 'expert_speak', 'feature', 'case_study', 'news', 'the_igtb_series', 'trend', 'white_paper')) )
        GROUP BY n.nid 
        ORDER BY pdate DESC)
    ) as UnionTable 
ORDER BY pdate DESC limit 10

【讨论】:

  • 你确定这个工作吗?你不需要另一个SELECT 作为标题吗?并为子查询添加别名?
  • @JuanCarlosOropeza 谢谢!!
  • @JuanCarlosOropeza 感谢您的回复。我已经尝试过了,但没有工作。请参阅我的问题更新 2。
【解决方案3】:

我不确定为什么需要union。而且,我不完全理解第三列的值应该是什么,因为它不是聚合函数的参数,也不在 group by 中。

但是,我会将查询写为:

(SELECT n.nid, max(na.gid) as mid, fav.field_date_posted_value as pdate
 FROM `node` n JOIN
      nodeaccess na
      ON na.nid = n.nid LEFT JOIN
      field_data_field_date_posted fav
      ON fav.entity_id = n.nid
 WHERE (na.gid IN(10,11) AND (n.status = '1') AND (n.type IN  ('article','blog', 'events', 'media', 'press_releases', 'expert_speak', 'feature', 'case_study', 'news', 'the_igtb_series', 'trend', 'white_paper')) )
 GROUP BY n.nid 
 ORDER BY pdate DESC
 LIMIT 10
) UNION ALL
(SELECT n.nid, max(na.gid) as mid, fav.field_date_posted_value as pdate
FROM `node` n JOIN
     nodeaccess na
     ON na.nid = n.nid LEFT JOIN
     field_data_field_date_posted fav
     ON fav.entity_id = n.nid
 WHERE (na.gid IN(2) AND (n.status = '1') AND (n.type IN  ('article','blog', 'events', 'media', 'press_releases', 'expert_speak', 'feature', 'case_study', 'news', 'the_igtb_series', 'trend', 'white_paper')) )
 GROUP BY n.nid 
 ORDER BY pdate DESC
 LIMIT 10
)
ORDER BY pdate DESC
LIMIT 10;

注意事项:

  • 将子查询限制为 10 行。为什么要处理比需要更多的数据?
  • 在子查询后使用ORDER BY 对所有结果进行排序。
  • 使用UNION ALL 而不是UNION,这样您的查询就不会产生删除重复项的开销。

【讨论】:

    【解决方案4】:

    创建一个额外的列,以便您可以订购每个 UNION 查询

    Sql Fiddle Demo

    SELECT '1' as grp, 1 as ID
    UNION 
    SELECT '1' as grp, 3 as ID
    UNION
    SELECT '1' as grp, 2 as ID
    UNION
    SELECT '1' as grp, 5 as ID
    UNION 
    SELECT '2' as grp, 4 as ID
    UNION 
    SELECT '2' as grp, 7 as ID
    UNION
    SELECT '2' as grp, 2 as ID
    UNION
    SELECT '2' as grp, 5 as ID
    
    ORDER BY grp, ID
    

    输出

    【讨论】:

      【解决方案5】:

      可以删除两个额外的 ORDER BY 子句。实际上很少有数据库只在 Union 语句之前放置 ORDER BY OR LIMIT 子句。您可能想在下面尝试:

          (
      (SELECT n.nid, max(na.gid) as mid, fav.field_date_posted_value as pdate FROM `node` as n
      JOIN nodeaccess AS na ON na.nid = n.nid
      LEFT JOIN field_data_field_date_posted AS fav ON fav.entity_id = n.nid
      WHERE (na.gid IN(10,11) AND (n.status = '1') AND (n.type IN  ('article','blog', 'events', 'media', 'press_releases', 'expert_speak', 'feature', 'case_study', 'news', 'the_igtb_series', 'trend', 'white_paper')) )
      GROUP BY n.nid 
       )
      
      UNION
      
      (SELECT n.nid, max(na.gid) as mid, fav.field_date_posted_value as pdate FROM `node` as n
      JOIN nodeaccess AS na ON na.nid = n.nid
      LEFT JOIN field_data_field_date_posted AS fav ON fav.entity_id = n.nid
      WHERE (na.gid IN(2) AND (n.status = '1') AND (n.type IN  ('article','blog', 'events', 'media', 'press_releases', 'expert_speak', 'feature', 'case_study', 'news', 'the_igtb_series', 'trend', 'white_paper')) )
      GROUP BY n.nid) 
      ) order by pdate DESC limit 10
      

      【讨论】:

        【解决方案6】:

        我使用临时表来解决联合上的排序问题:

        CREATE TEMPORARY TABLE IF NOT EXISTS temp_table1 AS 
        (  
        SELECT *   
        FROM  table1   
        ORDER BY < your specific order > )  
        
        CREATE TEMPORARY TABLE IF NOT EXISTS temp_table2 AS 
        (  
        SELECT *   
        FROM table2    
        ORDER BY < your specific order > )  
        
        SELECT * FROM temp_table1   
        UNION ALL  
        SELECT * FROM temp_table2   
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-05-30
          • 2010-10-04
          • 2011-12-20
          • 1970-01-01
          • 2017-05-09
          • 1970-01-01
          • 1970-01-01
          • 2014-10-03
          相关资源
          最近更新 更多