通过此stack overflow question,我们可以获得最大值为
的行select yt1.*
from yourtable yt1
left outer join yourtable yt2
on (yt1.id = yt2.id and yt1.rev < yt2.rev)
where yt2.id is null;
并假设上述结果给出了
+------+-------+--------------------------------------+
| id | rev | content |
+------+-------+--------------------------------------+
| 1 | 3 | 15 |
| 2 | 1 | 18 |
| 4 | 2 | 14 |
+------+-------+--------------------------------------+
我正在加入另一个包含上述id
primary key
+------+-------+-------------+
| id | myId | type |
+------+-------+-------------+
| 1 | 1 | 1 |
| 2 | 1 | 1 |
| 3 | 2 | 1 |
| 4 | 2 | 1 |
+------+-------+-------------+
as
select bt.myId, yt1.*
from baseTable bt
inner join yourtable yt1 (on bt.id=yt1.id)
left outer join yourtable yt2
on (yt1.id = yt2.id and yt1.rev < yt2.rev)
where yt2.id is null;
,连接将结果显示为
+------+------+-------+-----------------------+
| myId | id | rev | content |
+------+------+-------+-----------------------+
| 1 | 1 | 3 | 15 |
| 1 | 2 | 1 | 18 |
| 2 | 4 | 2 | 14 |
+------+------+-------+-----------------------+
现在,如何根据myId
获取内容值最大的行。即,最终输出将是集合[1,2,1,18]
和[2,4,2,14]
。它可以通过创建第一个结果的视图并再次对视图执行相同类型的查询来实现。有什么方法没有视图???