Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.
Original topic: tidb5.2 查询数据,数值总变(程序已经全部关闭,没有其他人连接数据库)
【TiDB Usage Environment】Production
【TiDB Version】5.2
【Encountered Problem】Querying data, values keep changing (all programs have been closed, no one else is connected to the database)
【Reproduction Path】Operations performed that led to the problem: Query operation
【Problem Phenomenon and Impact】
Is it possible that not all fields are covered in the group by clause?
This has nothing to do with group by. Even without group by, querying the same record at different times yields different values.
Could you post a few individual query results for us to take a look?
This is a query statement. Except for the time which is datetime, all others are varchar.
Here is the query statement I posted. Could you write a query for me to try?
It’s normal for the data to change. If the fields not included in the group by clause don’t use aggregate functions, aren’t they taken randomly?
Where did you post that single-row query without the group by clause?
Yes, it matters. According to development standards, fields outside of aggregate functions must be included in the GROUP BY clause.
In the TiDB test environment, there are no issues with the query, but in the production environment, it looks like this. Here are the screenshots without the group by clause:
In the test environment, with the same data and the same query statement, the query results are consistent.
Regarding development standards, we strictly require that the in the group by clause must be consistent with the in the select clause.
Could you provide the table structure and desensitized reproducible data? We will try to reproduce it.
Try removing the aggregate function (max) and see the result. It must be randomly selected.