选择具有不同值和最大日期的行
我有一个包含三列的 MySQL (5.1.49) 表。
mysql> create table testme(id int auto_increment, id2 int not null, somedate datetime, primary key(id));
就我而言, id2 不是唯一的,但我想返回具有不同 id2 值且最大 somedate 的行。
这是一些示例数据。
mysql> insert into testme values (1, 5, '2012-01-02 01:01:01'),(2, 5, '2012-02-02 02:02:02'),(3, 7, '2010-01-10 11:01:33');
这个问题几乎回答了我的问题,但还有额外的问题id 字段,返回的 id 和 id2 不匹配。对于 id2=5,它返回 id=1 而不是 id=2。
mysql> select id, id2, max(somedate) from testme group by id2;
+----+-----+---------------------+
| id | id2 | max(somedate) |
+----+-----+---------------------+
| 1 | 5 | 2012-02-02 02:02:02 |
| 3 | 7 | 2010-01-10 11:01:33 |
+----+-----+---------------------+
我期待着,
+----+-----+---------------------+
| id | id2 | max(somedate) |
+----+-----+---------------------+
| 2 | 5 | 2012-02-02 02:02:02 |
| 3 | 7 | 2010-01-10 11:01:33 |
+----+-----+---------------------+
想要与每个 ID2 的最大日期相匹配的 ID
有谁有什么想法吗?谢谢
I have a MySQL (5.1.49) table with three columns.
mysql> create table testme(id int auto_increment, id2 int not null, somedate datetime, primary key(id));
In my case id2 is not unique, but I want to return rows with distinct id2 values with the max somedate.
Here's some sample data.
mysql> insert into testme values (1, 5, '2012-01-02 01:01:01'),(2, 5, '2012-02-02 02:02:02'),(3, 7, '2010-01-10 11:01:33');
This question almost answers mine, but with the extra id field, the returned id and id2 don't match. For id2=5, it's returning id=1 instead of id=2.
mysql> select id, id2, max(somedate) from testme group by id2;
+----+-----+---------------------+
| id | id2 | max(somedate) |
+----+-----+---------------------+
| 1 | 5 | 2012-02-02 02:02:02 |
| 3 | 7 | 2010-01-10 11:01:33 |
+----+-----+---------------------+
I'm expecting,
+----+-----+---------------------+
| id | id2 | max(somedate) |
+----+-----+---------------------+
| 2 | 5 | 2012-02-02 02:02:02 |
| 3 | 7 | 2010-01-10 11:01:33 |
+----+-----+---------------------+
Want the ID that matches maximum date for each ID2
Does anyone have any ideas please? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这个查询肯定会起作用,尽管它可能不是最佳的:
This query would definitely work, although it may be not the optimal one:
问题的替代解决方案:
像@itsmeee解决方案一样,如果 id2, somedate 对不唯一,它将带来两行:
如果 id2, somedate 对不唯一并且,由于某种原因,您只需要每个
id2
一个结果,您可以让 MySQL 通过额外的GROUP BY t0.id2
子句为您选择一个结果。但请注意,这是非标准 SQL 行为。An alternative solution to your problem:
Like @itsmeee solution, if
id2, somedate
pairs are not unique, it will bring both rows:If
id2, somedate
pairs are not unique and, for some reason, you need only one result perid2
, you can let MySQL pick one for you with an extraGROUP BY t0.id2
clause. But be warned that this is non standard SQL behavior.当要获取一组记录中的最大日期时,不建议将
max(date)
与其他行组合。当然,您总是会得到max(date)
但其他字段将是该集合中的第一个出现的字段。以下是您的问题的答案,以您的表格为例:When it is about getting the maximum date in a set of record, it is not advisable to combine
max(date)
with other rows. You will always get, of course, themax(date)
but other fields will be the first occurrence in that set. Here is an answer to your question, using your table as an example:不确定,但根据您的示例,请尝试以下操作:
Not sure, but based on your example, try this: