[英]Returning max from query
I'm trying to create a query to return the file with the max version, independent of the value of the server. 我试图创建一个查询以返回具有最高版本的文件,而与服务器的值无关。 How could I do that? 我该怎么办?
actual table data: 实际表数据:
server filename v4 date
local code1.zip 41 0000-00-00
remote code1.zip 39 0000-00-00
local code1.zip 28 0000-00-00
remote code1.zip 21 0000-00-00
local code1.zip 32 0000-00-00
remote code1.zip 27 0000-00-00
the query: 查询:
SELECT
server,
filename,
max(v4) as v4,
date
FROM
table
WHERE
date ='0000-00-00'
GROUP BY
filename,
server,
date
Actual result: 实际结果:
server filename v4 date
local code1.zip 41 0000-00-00
remote code1.zip 39 0000-00-00
Expected result: 预期结果:
server filename v4 date
local code1.zip 41 0000-00-00
EDIT: It's for MySQL 编辑:这是为MySQL
Thanks in advance. 提前致谢。
If you just want the row with largest v4 you can use this 如果您只想要最大v4的行,则可以使用此
SELECT
server,
filename,
v4,
date
FROM
`table`
WHERE
date ='0000-00-00'
ORDER BY v4 DESC
LIMIT 1
to get max V4 for each filename, first you get max(v4) group by filename then INNER JOIN back with table
like below 要获得每个文件名的最大V4,首先按文件名获得max(v4)组,然后使用如下table
进行INNER JOIN返回
SELECT T1.server,T1.filename,T1.v4,T1.date
FROM
`table` T1 INNER JOIN
(SELECT filename,max(v4) as maxv4
FROM `table`
WHERE date = '0000-00-00'
GROUP BY filename)T2
ON T1.filename = T2.filename AND T1.v4 = T2.maxV4
WHERE date = '0000-00-00';
To get the the max version you only have to do: 要获得最高版本,您只需要执行以下操作:
SELECT MAX(version) FROM table
If you also want the filename (if you have another column id
): 如果您还想要文件名(如果您还有另一个列id
):
SELECT t2.filename, t2.maxver
FROM table t1 INNER JOIN
(SELECT id, MAX(version) maxver
FROM table
GROUP BY id) t2 ON t1.id=t2.id
ORDER BY t2.maxver DESC
LIMIT 1
If you also want the filename (if you haven't another column id
, use filename
): 如果您还想要文件名(如果没有其他的列id
,请使用filename
):
SELECT t2.filename, t2.maxver
FROM table t1 INNER JOIN
(SELECT filename, MAX(version) maxver
FROM table
GROUP BY filename) t2 ON t1.filename=t2.filename
WHERE t2.maxver = (SELECT MAX(version) FROM table)
There are many ways to do it. 有很多方法可以做到这一点。
You could use this query: 您可以使用以下查询:
Select Top 1 *
From table
Where v4 = (
SELECT
max(v4) as v4,
FROM
table
WHERE
date ='0000-00-00'
GROUP BY
v4
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.