Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何从一组复合行中查找最大值?_Mysql_Sql_Max_Composite - Fatal编程技术网

Mysql 如何从一组复合行中查找最大值?

Mysql 如何从一组复合行中查找最大值?,mysql,sql,max,composite,Mysql,Sql,Max,Composite,我有一张桌子,比如: Year | Month ------------ 2011 10 2011 11 2012 5 2012 6 查询应返回最近一年的最近月份 目前我正在做一些类似的事情 从表中选择MAXMonth,其中年份在表中选择MAXYear 但我对这个问题不满意。有人能建议一种更紧凑、更干净的方法吗 试试这个 select top 1 "Month" from table order by "Year" desc, "Month"

我有一张桌子,比如:

Year | Month
------------
2011    10   
2011    11   
2012    5   
2012    6 
查询应返回最近一年的最近月份

目前我正在做一些类似的事情

从表中选择MAXMonth,其中年份在表中选择MAXYear

但我对这个问题不满意。有人能建议一种更紧凑、更干净的方法吗

试试这个

select top 1
    "Month"
from table
order by "Year" desc, "Month" desc
好吧,对于MySQL,我想应该是这样

select
    "Month"
from table
order by "Year" desc, "Month" desc
limit 1
试试这个:

select t1.months from
(select top 1 t.months as months,max(t.years) as years from
(select years,max(months) as months from cal group by years) t
group by t.months) t1

我认为除了在声明中的问题外,你的问题已经很清楚了。我认为select MAXYear返回一个单一值,因此您需要一个=符号,而不是in。是的,我更喜欢SQL Server,我已经为MySQL编辑了文章,建议使用MySQL,我不能尝试它-没有MySQL访问权限,除了标识符引号,在MySQL中应该使用反引号,而不是双引号,表示字符串文字,这看起来不错。