假设有这样一张表。
id town_id begin_date
12 2 2011-10-10
23 2 2011-11-10
43 2 2012-01-01
现在如果我这样做
SELECT id, MAX(begin_date) AS mx
FROM regions
上面的查询返回最大日期但是id是错误的:
id mx
12 2012-01-01
这是预期的吗?
我怎样才能让它返回正确的 id (43, 2012-01-01)
最佳答案
如果您要做的是获取与 MAX() 日期关联的 ID,那么您可以:
SELECT id, begin_date from regions order by begin_date DESC LIMIT 1;
关于mysql - 使用 max 时 select 返回错误的 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8736885/