[英]Mysql multiple ORDER BY with UNION
I want to get data with order by date. 我想按日期获取数据。 I am using union.
我正在使用工会。
I have gone through the below url. 我已经浏览了以下网址。 But I am not getting clear solution with that.
但是我对此并没有明确的解决方案。
mysql order by with union doesn't seem to work mysql order by union似乎不起作用
My Query 我的查询
(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
My Result 我的结果
+-------+------+---------------------+
| 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 |
+-------+------+---------------------+
In the above url the date order is not working. 在上述网址中,日期顺序无效。 I don't want to make order by as common.
我不想像普通人那样下订单。 I need separate order by for both select queries (Used in UNION).
我需要为两个选择查询使用单独的顺序(在UNION中使用)。
Update 2: 更新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
The above gives the below result. 上面给出了以下结果。
+-------+------+---------------------+
| 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 |
+-------+------+---------------------+
In this result I don't want to reorder the column " mid " 在此结果中,我不想对“ mid ”列进行重新排序
You need to wrap the outcome of union of both select statements and order it by pdate
. 您需要包装两个select语句的并集结果,并按
pdate
对其进行排序。
as below: 如下:
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
I'm not sure why a union
is even needed. 我不确定为什么还需要
union
。 And, I don't fully understand what the third column value is supposed to be, because it is not the argument to an aggregation function and is not in the group by
. 而且,我不完全理解第三列的值应该是什么,因为它不是聚合函数的参数,也不在
group by
。
However, I would write the query as: 但是,我将查询编写为:
(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;
Notes: 笔记:
ORDER BY
after the subqueries to order all the results. ORDER BY
对所有结果进行排序。 UNION ALL
instead of UNION
so your query does not incur the overhead of removing duplicates. UNION ALL
而不是UNION
这样您的查询就不会产生删除重复项的开销。 Create one additional column so you can order each UNION
query 再创建一列,以便可以对每个
UNION
查询进行排序
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
OUTPUT OUTPUT
Two additional ORDER BY Clause can be removed. 可以删除另外两个ORDER BY子句。 In fact few DB only to put ORDER BY OR LIMIT clause before Union statement.
实际上很少有DB仅将ORDER BY OR LIMIT子句放在Union语句之前。 You might want to try below:
您可能要尝试以下操作:
(
(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
Try this 尝试这个
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
Fixed the order by date only base or group 固定仅按日期或组排序的日期
I use temporary tables to resolve ordering issues on union: 我使用临时表解决联合上的订购问题:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.