mysql - 尝试根据日期查找表的最大行时得到奇怪的值

标签 mysql sql database

我正在尝试做一些我认为相对简单的事情。获取用户最后输入的值。 我有一个表,用于跟踪他们所有名为 plan_activities_logs 的条目。我使用此语句获取有关单个用户的所有事件:

SELECT created_at as last_active, plan_id, plan_value 
from plan_activity_logs where plan_id IN (select id from plans where tile_id = 30);

它给了我一个看起来像这样的表:enter image description here

但是当我尝试做这样的事情时:

SELECT MAX(created_at) as last_active, plan_id, plan_value from plan_activity_logs where plan_id IN (select id from plans where tile_id = 30);

我明白了: enter image description here

虽然日期和 ID 正确,但计划值是错误的值。知道我做错了什么吗?

最佳答案

Any idea what I'm doing wrong?

你没有做错任何事。根据the manual

If ONLY_FULL_GROUP_BY is disabled, a MySQL extension to the standard SQL use of GROUP BY permits the select list, HAVING condition, or ORDER BY list to refer to nonaggregated columns even if the columns are not functionally dependent on GROUP BY columns. This causes MySQL to accept the preceding query. In this case, the server is free to choose any value from each group, so unless they are the same, the values chosen are nondeterministic, which is probably not what you want.

换句话说,这个行为是符合规范的。查询返回一个随机的非确定性值,因为您正在使用 MySql 非标准扩展来分组查询。

关于mysql - 尝试根据日期查找表的最大行时得到奇怪的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51900380/

相关文章:

mysql - 包含用户已保存产品的所有产品列表

java - 在 Android 中使用 Java 插入 FOR 循环内的 SQLITE TABLE 得到 NPE 错误?

MySQL utf8mb4 外键错误

php - Laravel 在 MySQL 列上使用 IF 子句

php - 如何以 orderby rand 限制为 6 获得相等的记录?

php - 将多维php数组插入mysql表时如何添加id(主键)?

sql - MySQL -> RESTful API

java - 处理 DATETIME 值 0000-00-00 00 :00:00 in JDBC

sql - 创建 DB2 历史表触发器

c# - 销售人员记录