Py学习  »  DATABASE

如何在mysql中使用group by查询未聚合的列?

Aniruddha Raje • 7 年前 • 2235 次点击  

我有一个用例,想获取用户前一天所做的最新交易,
如果今天是2019年6月26日,
我想去拿 the last transaction made by the user on 25th June 2019 at 23:59:59

我的数据是这样的-

enter image description here

我不擅长sql并尝试了以下查询-

SELECT MAX(transaction_id), user_id, MAX(created_date), new_balance  
FROM transaction
where created_date < 1561507199 
GROUP BY user_id;

我得到以下错误-

Error Code: 1055. Expression #4 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'transaction.new_balance' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/47476
文章 [ 1 ]  |  最新文章 7 年前