表'测试'
+------+--------------------+
|value | dateTime |
+------+--------------------+
|19 |2011-12-22 11:09:42 |
+------+--------------------+
|16 |2011-12-22 05:09:00 |
+------+--------------------+
|2 |2011-12-22 07:09:42 |
+------+--------------------+
|30 |2011-12-22 10:09:15 |
+------+--------------------+
|45 |2011-12-22 03:09:42 |
+------+--------------------+
我想获取Max值及其对应的dateTime。在这种情况下,最终结果应为:最大值:45和dateTime:2011-12-22 03:09:42 。我用了
SELECT MAX( `value` ) , `dateTime`
FROM `test`
运行上面的sql语句后,最大'值'是正确的(即45),但'dateTime'不正确(ie2011-12-22 11:09:19)
有没有人可以帮助我?非常感谢!
答案 0 :(得分:3)
试试这个:
SELECT TOP 1 `value`, `dateTime`
FROM `test`
ORDER BY `value` DESC
答案 1 :(得分:2)
-- general
select *
from test
where value= (select max(value) from test)
或
--tsql
select top 1 *
from test
order by value desc
或
--mysql
select *
from test
order by value desc
limit 1;
答案 2 :(得分:0)
版本5.5中的MySQL存在错误。尝试升级MySQL。你的代码不是问题。