在MYSQL查询中,如何选择值最高的所有行,例如:
SELECT UNIX_TIMESTAMP(creation_date) AS `date`,
release_version
FROM content
WHERE id = '1'
ORDER BY `date` DESC
这将输出例如:
注意:具有相同/不同release_version
的行数是可变的,因此我无法使用LIMIT。
我只想选择release_version
最高的那些(本例中为3)
我该怎么做?感谢。
答案 0 :(得分:4)
这可以简单地作为
SELECT *
FROM content
WHERE release_version = (SELECT MAX(release_version) FROM content)
答案 1 :(得分:1)
使用HAVING clausule。
SELECT UNIX_TIMESTAMP(creation_date) AS `date`,
MAX(`release_version`) as max,
FROM content
WHERE id = '1'
HAVING max > 3
ORDER BY `date` DESC
修改强>
或者你可以使用
SELECT UNIX_TIMESTAMP(creation_date) AS `date`,
`release_version` as max,
FROM content
WHERE id = '1' AND release_version = (SELECT MAX(`release_version`) FROM content WHERE id = '1')
ORDER BY `date` DESC