[英]Mysql multiple ORDER BY with 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( 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 |
+-------+------+---------------------+
在上述网址中,日期顺序无效。 我不想像普通人那样下订单。 我需要为两个选择查询使用单独的顺序(在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 ”列进行重新排序
您需要包装两个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
我不确定为什么还需要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;
笔记:
ORDER BY
对所有结果进行排序。 UNION ALL
而不是UNION
这样您的查询就不会产生删除重复项的开销。 再创建一列,以便可以对每个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
可以删除另外两个ORDER BY子句。 实际上很少有DB仅将ORDER BY OR LIMIT子句放在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(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
尝试这个
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
固定仅按日期或组排序的日期
我使用临时表解决联合上的订购问题:
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.