根据另一列中的最大值选择列

时间:2011-06-28 16:24:35

标签: mysql sql

我们假设有一个名为test的表格,其中有两列 - idtime id列不是唯一的

我希望获得所有ID,其中任何行的ID都不超过 X

怎么做?

5 个答案:

答案 0 :(得分:3)

SELECT id FROM test
GROUP BY id
HAVING MAX(time) <= 'X'

答案 1 :(得分:1)

这应该适合您的需要:

SELECT id 
FROM tableName 
WHERE time > '{X}' AND NOT ID = '{ID}'
GROUP BY id;

答案 2 :(得分:1)

select distinct id
from table t
where t.id not in (select it.id
                   from table it
                   where it.time>@x)

答案 3 :(得分:0)

SELECT t.id 
FROM table t
WHERE NOT EXISTS
  ( SELECT * 
    FROM table tt
    WHERE tt.time > X
      AND tt.id = t.id
  )

答案 4 :(得分:0)

SELECT id FROM (
    SELECT max(time) as max_time, id
    FROM test
    GROUP BY id
) tmp
WHERE max_time < 'X'