[英]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.