按组内排序

发布于 2024-10-18 21:18:13 字数 3398 浏览 5 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

垂暮老矣 2024-10-25 21:18:13

我相信您可以在 GROUP_CONCAT 函数中指定 ORDER BY 子句:

SELECT RunGroupID, GROUP_CONCAT(DNAID, DNBID ORDER BY DNAID, DNBID SEPARATOR '')

I believe you can specify an ORDER BY clause in the GROUP_CONCAT function:

SELECT RunGroupID, GROUP_CONCAT(DNAID, DNBID ORDER BY DNAID, DNBID SEPARATOR '')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文