按组内排序
order by 在第二个查询中不起作用。 我需要先按 DNAID 排序,然后按 DNBID
第一个查询排序,其顺序为: 111221
第二个查询其顺序为: 112112
了解有关我想要完成的任务的更多信息和细节 https://stackoverflow.com/questions/5082880/database-query-group-union-查找最新
mysql> select * from metarun;
+----+------------+-------+-------+--------------+----------+
| ID | RunGroupID | DNAID | DNBID | CONFIGTYPEID | DateTime |
+----+------------+-------+-------+--------------+----------+
| 1 | 1 | 1 | 1 | 2 | NULL |
| 2 | 1 | 1 | 2 | 2 | NULL |
| 3 | 1 | 2 | 1 | 2 | NULL |
| 4 | 2 | 1 | 1 | 4 | NULL |
| 5 | 2 | 3 | 2 | 4 | NULL |
| 6 | 3 | 3 | 2 | 1 | NULL |
| 7 | 3 | 1 | 2 | 1 | NULL |
| 8 | 3 | 3 | 1 | 1 | NULL |
| 9 | 4 | 1 | 1 | 3 | NULL |
| 10 | 4 | 2 | 1 | 3 | NULL |
| 11 | 4 | 1 | 2 | 3 | NULL |
| 12 | 5 | 1 | 1 | 2 | NULL |
| 13 | 5 | 2 | 1 | 2 | NULL |
| 14 | 5 | 1 | 2 | 2 | NULL |
| 15 | 6 | 3 | 2 | 4 | NULL |
| 16 | 6 | 1 | 1 | 4 | NULL |
+----+------------+-------+-------+--------------+----------+
mysql> SELECT RunGroupID, GROUP_CONCAT(DNAID, DNBID SEPARATOR ''), ConfigTypeID, DateTime
-> FROM metarun
-> GROUP BY RunGroupID
-> ORDER BY RunGroupID, DNAID ASC, DNBID ASC;
+------------+-----------------------------------------+--------------+----------+
| RunGroupID | GROUP_CONCAT(DNAID, DNBID SEPARATOR '') | ConfigTypeID | DateTime |
+------------+-----------------------------------------+--------------+----------+
| 1 | 111221 | 2 | NULL |
| 2 | 1132 | 4 | NULL |
| 3 | 123132 | 1 | NULL |
| 4 | 111221 | 3 | NULL |
| 5 | 111221 | 2 | NULL |
| 6 | 1132 | 4 | NULL |
+------------+-----------------------------------------+--------------+----------+
6 rows in set (0.00 sec)
mysql> SELECT RunGroupID, GROUP_CONCAT(DNAID, DNBID SEPARATOR ''), ConfigTypeID, DateTime
-> FROM metarun
-> WHERE configtypeid=2
-> GROUP BY RunGroupID
-> ORDER BY RunGroupID;
+------------+-----------------------------------------+--------------+----------+
| RunGroupID | GROUP_CONCAT(DNAID, DNBID SEPARATOR '') | ConfigTypeID | DateTime |
+------------+-----------------------------------------+--------------+----------+
| 1 | 111221 | 2 | NULL |
| 5 | 112112 | 2 | NULL |
+------------+-----------------------------------------+--------------+----------+
2 rows in set (0.02 sec)
The order by is not working in the second query.
I need to order by first DNAID then DNBID
First Query its ordered as:
111221
Second Query its ordered as:
112112
for more info and details about what im trying to accomplish
https://stackoverflow.com/questions/5082880/database-query-group-union-find-latest
mysql> select * from metarun;
+----+------------+-------+-------+--------------+----------+
| ID | RunGroupID | DNAID | DNBID | CONFIGTYPEID | DateTime |
+----+------------+-------+-------+--------------+----------+
| 1 | 1 | 1 | 1 | 2 | NULL |
| 2 | 1 | 1 | 2 | 2 | NULL |
| 3 | 1 | 2 | 1 | 2 | NULL |
| 4 | 2 | 1 | 1 | 4 | NULL |
| 5 | 2 | 3 | 2 | 4 | NULL |
| 6 | 3 | 3 | 2 | 1 | NULL |
| 7 | 3 | 1 | 2 | 1 | NULL |
| 8 | 3 | 3 | 1 | 1 | NULL |
| 9 | 4 | 1 | 1 | 3 | NULL |
| 10 | 4 | 2 | 1 | 3 | NULL |
| 11 | 4 | 1 | 2 | 3 | NULL |
| 12 | 5 | 1 | 1 | 2 | NULL |
| 13 | 5 | 2 | 1 | 2 | NULL |
| 14 | 5 | 1 | 2 | 2 | NULL |
| 15 | 6 | 3 | 2 | 4 | NULL |
| 16 | 6 | 1 | 1 | 4 | NULL |
+----+------------+-------+-------+--------------+----------+
mysql> SELECT RunGroupID, GROUP_CONCAT(DNAID, DNBID SEPARATOR ''), ConfigTypeID, DateTime
-> FROM metarun
-> GROUP BY RunGroupID
-> ORDER BY RunGroupID, DNAID ASC, DNBID ASC;
+------------+-----------------------------------------+--------------+----------+
| RunGroupID | GROUP_CONCAT(DNAID, DNBID SEPARATOR '') | ConfigTypeID | DateTime |
+------------+-----------------------------------------+--------------+----------+
| 1 | 111221 | 2 | NULL |
| 2 | 1132 | 4 | NULL |
| 3 | 123132 | 1 | NULL |
| 4 | 111221 | 3 | NULL |
| 5 | 111221 | 2 | NULL |
| 6 | 1132 | 4 | NULL |
+------------+-----------------------------------------+--------------+----------+
6 rows in set (0.00 sec)
mysql> SELECT RunGroupID, GROUP_CONCAT(DNAID, DNBID SEPARATOR ''), ConfigTypeID, DateTime
-> FROM metarun
-> WHERE configtypeid=2
-> GROUP BY RunGroupID
-> ORDER BY RunGroupID;
+------------+-----------------------------------------+--------------+----------+
| RunGroupID | GROUP_CONCAT(DNAID, DNBID SEPARATOR '') | ConfigTypeID | DateTime |
+------------+-----------------------------------------+--------------+----------+
| 1 | 111221 | 2 | NULL |
| 5 | 112112 | 2 | NULL |
+------------+-----------------------------------------+--------------+----------+
2 rows in set (0.02 sec)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信您可以在
GROUP_CONCAT
函数中指定ORDER BY
子句:I believe you can specify an
ORDER BY
clause in theGROUP_CONCAT
function: