简体   繁体   English

如何在mysql的最大值内获取最大值?

[英]How can I get the max value within a max value in mysql?

I have a schema like the following 我有一个类似以下的架构

id (INT)
Cycle_Number (INT)
Cycle_Day (INT)
Date (date)
...other columns irrelevant to the question... 与该问题无关的其他列

How can I get the row that has the max Cycle_Day within the max Cycle_Number 如何获取最大Cycle_Number内具有最大Cycle_Day的行

For example, say I have the following data 例如,说我有以下数据

 ID Cycle_Number Cycle_Day Date 1 1 1 2011-12-01 2 1 2 2011-12-02 3 2 1 2011-12-03 4 2 2 2011-12-04 5 2 3 2011-12-05 6 2 4 2011-12-06 7 3 1 2011-12-07 8 3 2 2011-12-08 9 3 3 2011-12-09 

The query would return row 9. (It has the highest Cycle_Day within the highest Cycle_Number) 该查询将返回第9行。(它在最高Cycle_Number中具有最高Cycle_Day)

Thanks 谢谢

this one is compatible MySql 5.5 with no joint tables 这是兼容的MySql 5.5,没有联合表

SELECT id
FROM cycles
ORDER BY Cycle_Number DESC , Cycle_Day DESC
LIMIT 0 , 1

Regards 问候

This SQL query should provide the max value you want. 此SQL查询应提供所需的最大值。

SELECT ID, Cycle_Number, Cycle_Day, Date
FROM  yourTable AS t
CROSS JOIN (
  SELECT MAX(Cycle_Number) AS Cycle_Number FROM yourTable
) AS sq USING (Cycle_Number)
ORDER BY Cycle_Day DESC LIMIT 1

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

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