Mysql中按日期排序和分组

发布于 2024-10-12 00:20:37 字数 624 浏览 3 评论 0原文

大家好,提前致谢。 我有一个无法解决的小问题,我有这个表,我想按日期排序并将其分组(但我只显示每个 idCAT 1 行)

| id | idcat | name |     date   |

| 1  | 3     | xx   | 2011-01-02 |

| 2  | 4     | xf   | 2011-01-02 |

| 3  | 3     | cd   | 2011-01-01 |

| 4  | 1     | cg   | 2011-01-04 |

| 5  | 4     | ce   | 2011-01-06 |

想保持这种方式,尝试某种方式,但我不能

| 2  | 4     | xf   | 2011-01-02 |

| 3  | 3     | cd   | 2011-01-01 |

| 4  | 1     | cg   | 2011-01-04 |

按 ID 订购


感谢一位朋友的工作。

SELECT id, idcat, name, date FROM (SELECT * FROM data ORDER BY idcat, date ) m GROUP BY idcat

Hi all and thanks in advance.
I have a small problem which can not resolve, I have this table, and I want to sort by date and group it (but I only show 1 row per idCAT)

| id | idcat | name |     date   |

| 1  | 3     | xx   | 2011-01-02 |

| 2  | 4     | xf   | 2011-01-02 |

| 3  | 3     | cd   | 2011-01-01 |

| 4  | 1     | cg   | 2011-01-04 |

| 5  | 4     | ce   | 2011-01-06 |

would like to stay that way, try in a way but I can not

| 2  | 4     | xf   | 2011-01-02 |

| 3  | 3     | cd   | 2011-01-01 |

| 4  | 1     | cg   | 2011-01-04 |

Order by ID


Thank's a one friend the work.

SELECT id, idcat, name, date FROM (SELECT * FROM data ORDER BY idcat, date ) m GROUP BY idcat

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

你是年少的欢喜 2024-10-19 00:20:37

我无法方便地测试 atm,但请尝试以下操作:

SELECT FIRST(id), idcat, FIRST(name), FIRST(date) AS d FROM myTable GROUP BY idcat ORDER BY d;

请注意使用 FIRST 调用来选择具有任何特定 idcat 的表中的第一行。

I can't test conveniently atm, but try this:

SELECT FIRST(id), idcat, FIRST(name), FIRST(date) AS d FROM myTable GROUP BY idcat ORDER BY d;

Note the use of the FIRST calls to pick the first row in the table with any particular idcat.

岁月无声 2024-10-19 00:20:37

如果您尝试按 idcat 进行分组,则按日期排序:

select id, idcat, name, date from myTable group by idcat order by date;

If you are trying to get groupings by the idcat, then sorted by date:

select id, idcat, name, date from myTable group by idcat order by date;

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