从一致记录中获取最大值

时间:2016-12-10 06:46:18

标签: mysql sql

我想从给定的一致数据列中获取最大值,如下所示:

id值

1  10 
2  20 
3  30 
4  40
5  50
6  60
7  70
8  80
9  1 
10 2
11 90
12 3
13 110
14 4 

输出将是80 ..我不知道如何尝试它。 你的帮助很明显。

6 个答案:

答案 0 :(得分:2)

 SELECT MAX(value) AS MAX FROM table_name;

答案 1 :(得分:1)

此解决方案纯粹基于您的数据,所以现在我发布这个以立即解决方案,我们也可以提出其他更好的方法:

SELECT 
    MAX(t1.val) maxValue
FROM tmp t
INNER JOIN tmp t1 ON t.id+1 = t1.id
WHERE t1.val-t.val = 10

答案 2 :(得分:1)

试试这个:

select id,value from (
  select 
  @last_id as id,  
  @last_value as value,
  case when @last_value is not null 
  then 
    case when @last_value<value 
    THEN
      @counter:=@counter+1
    ELSE
     CASE WHEN @counter!='Change Found' THEN @counter:='Change Found' END
    END
  else -1 
  end as `change`,
  @last_value:=value,
  @last_id:=id
  from Table1,(select @last_value:=null,@counter:=-1,@last_id:=null)p
)tbl where tbl.`change`='Change Found' limit 1;

SQL Fiddle Demo

答案 3 :(得分:1)

select      value

from       (select      id
                       ,case when value > @prev_value then value end    as value
                       ,@prev_value := value

            from        mytable

            order by    id desc
            ) t

where       value is not null

order by    id

limit       1
;

select      value

from       (select      id
                       ,case when value < @prev_value then @prev_value end  as value
                       ,@prev_value := value

            from        mytable

            order by    id
            ) t

where       value is not null

order by    id

limit       1
;

答案 4 :(得分:0)

在看到所选择的解决方案后,我建议 -

select 80;

答案 5 :(得分:-1)

您可以像这样使用HAVING子句:

select value 
from myTable
GROUP BY value -- forgot this one here
HAVING value = max(value)

或者我认为即便是最简单的

select max(value)
from myTable