使用SQL for MySql提取列中具有最大值而非值的行
我在mysql中有这样一个表:使用SQL for MySql提取列中具有最大值而非值的行,mysql,sql,Mysql,Sql,我在mysql中有这样一个表: +----+------+-----------+----+----+-----+ | id | vers | doc | pr | dd | f | +----+------+-----------+----+----+-----+ | 1 | 1 | doc1.pdf | 1 | 1 | neg | +----+------+-----------+----+----+-----+ | 2 | 1 | other.pdf
+----+------+-----------+----+----+-----+
| id | vers | doc | pr | dd | f |
+----+------+-----------+----+----+-----+
| 1 | 1 | doc1.pdf | 1 | 1 | neg |
+----+------+-----------+----+----+-----+
| 2 | 1 | other.pdf | 1 | 3 | pos |
+----+------+-----------+----+----+-----+
| 3 | 1 | bo.pdf | 1 | 6 | ok |
+----+------+-----------+----+----+-----+
| 4 | 2 | doc2.pdf | 1 | 1 | pos |
+----+------+-----------+----+----+-----+
如您所见,id为1和4的行具有相同的pr和dd值,但具有不同的vers
我希望检索所有行,如果我有更多具有相同pr和dd的行,我希望只获取vers列中具有最大值的行
所以在本例中,我想要id为2、3和4的行
我可以使用SQL查询来实现这一点吗?
如果是,怎么做?有一个子查询,返回每个pr/dd组合的最大值。加入这一结果:
select t1.*
from tablename t1
join (select pr, dd, max(vers) as vers
from tablename
group by pr, dd) t2
on t1.pr = t2.pr
and t1.dd = t2.dd
and t1.vers = t2.vers
按pr从foo组中选择MAXvers、pr、dd,dd为您提供了最大版本,然后您可以使用pr和dd将其加入到SELECT*FROM foo中,并保留vers=的行max@RC. 他想要整排人