选择具有不同值和最大日期的行

发布于 2025-01-01 17:46:47 字数 1322 浏览 1 评论 0原文

我有一个包含三列的 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 字段,返回的 idid2 不匹配。对于 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 技术交流群。

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

发布评论

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

评论(4

眉目亦如画i 2025-01-08 17:46:47

这个查询肯定会起作用,尽管它可能不是最佳的:

select t.id, s.id2, s.somedate 
from testme t 
  join 
    ( select id2, max(somedate) as somedate 
      from testme 
      group by id2
    ) s 
    on  s.id2 = t.id2 
    and s.somedate = t.somedate;

This query would definitely work, although it may be not the optimal one:

select t.id, s.id2, s.somedate 
from testme t 
  join 
    ( select id2, max(somedate) as somedate 
      from testme 
      group by id2
    ) s 
    on  s.id2 = t.id2 
    and s.somedate = t.somedate;
巾帼英雄 2025-01-08 17:46:47

问题的替代解决方案:

SELECT t0.id, t0.id2, t0.somedate 
FROM testme AS t0 
LEFT JOIN testme AS t1 
    ON t0.id2 = t1.id2 
    AND t1.somedate > t0.somedate
WHERE t1.id IS NULL;

像@itsmeee解决方案一样,如果 id2, somedate 对不唯一,它将带来两行:

+----+-----+---------------------+
| id | id2 | somedate            |
+----+-----+---------------------+
|  4 |   8 | 2012-02-02 02:02:02 |
|  6 |   8 | 2012-02-02 02:02:02 |
+----+-----+---------------------+

如果 id2, somedate 对不唯一并且,由于某种原因,您只需要每个 id2 一个结果,您可以让 MySQL 通过额外的 GROUP BY t0.id2 子句为您选择一个结果。但请注意,这是非标准 SQL 行为。

An alternative solution to your problem:

SELECT t0.id, t0.id2, t0.somedate 
FROM testme AS t0 
LEFT JOIN testme AS t1 
    ON t0.id2 = t1.id2 
    AND t1.somedate > t0.somedate
WHERE t1.id IS NULL;

Like @itsmeee solution, if id2, somedate pairs are not unique, it will bring both rows:

+----+-----+---------------------+
| id | id2 | somedate            |
+----+-----+---------------------+
|  4 |   8 | 2012-02-02 02:02:02 |
|  6 |   8 | 2012-02-02 02:02:02 |
+----+-----+---------------------+

If id2, somedate pairs are not unique and, for some reason, you need only one result per id2, you can let MySQL pick one for you with an extra GROUP BY t0.id2 clause. But be warned that this is non standard SQL behavior.

ゃ懵逼小萝莉 2025-01-08 17:46:47

当要获取一组记录中的最大日期时,不建议将 max(date) 与其他行组合。当然,您总是会得到 max(date) 但其他字段将是该集合中的第一个出现的字段。以下是您的问题的答案,以您的表格为例:

+----+-----+---------------------+
| id | id2 |    somedate         |
+----+-----+---------------------+
|  1 |   5 | 2012-01-02 01:01:01 |
|  2 |   5 | 2012-02-02 02:02:02 |
|  3 |   7 | 2010-01-10 11:01:33 |
+----+-----+---------------------+
select id, id2, somedate from testme t1
where somedate = 
(select max(somedate) from testme t2 where t2.somedate = t1.somedate) 
order by somedate desc

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, the max(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:

+----+-----+---------------------+
| id | id2 |    somedate         |
+----+-----+---------------------+
|  1 |   5 | 2012-01-02 01:01:01 |
|  2 |   5 | 2012-02-02 02:02:02 |
|  3 |   7 | 2010-01-10 11:01:33 |
+----+-----+---------------------+
select id, id2, somedate from testme t1
where somedate = 
(select max(somedate) from testme t2 where t2.somedate = t1.somedate) 
order by somedate desc
浊酒尽余欢 2025-01-08 17:46:47

不确定,但根据您的示例,请尝试以下操作:

select max(id) as Id,id2,max(someDate) from testMe group by Id2

Not sure, but based on your example, try this:

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