MYSQL选择值最高的所有地方

时间:2013-05-04 08:59:52

标签: mysql

在MYSQL查询中,如何选择值最高的所有行,例如:

SELECT      UNIX_TIMESTAMP(creation_date) AS `date`,
            release_version
FROM        content
WHERE       id = '1' 
ORDER BY    `date` DESC

这将输出例如:

output

注意:具有相同/不同release_version的行数是可变的,因此我无法使用LIMIT。

我只想选择release_version最高的那些(本例中为3)

我该怎么做?感谢。

2 个答案:

答案 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