简体   繁体   English

从 where 子句中的列中获取最大值

[英]Get highest value from column in where clause

I have table:我有表:

id date  day
2  21-07 1
2  10-07 3
2  11-07 2

I need to get date which has highest day , in current example it should be 10-07.我需要date具有最高的day ,在当前的例子应该是10-07。 Next query works fine, but is there way to make it more optimised?下一个查询工作正常,但有没有办法让它更优化?

SELECT date
      FROM (SELECT date, MAX(day)
            FROM some_table 
            WHERE id = 2
            GROUP BY date
        LIMIT 1) x

I would not suggest a where clause.我不建议使用where子句。 Just use:只需使用:

select date
from some_table
where id = 2
order by day desc
limit 1;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM