MySQL 错误代码:1055。SELECT 列表的表达式 #3 不在 GROUP BY > 子句中并且包含非聚合列
MySQL Error Code: 1055. Expression #3 of SELECT list is not in GROUP BY > clause and contains nonaggregated column
我有一个 "chat" table 可以记录 2 个人之间的聊天记录。我们称第一人称"speaker",第二人称"receiver"。
当第一个人发送聊天消息时,他将是"speaker"。第二个收到消息的人将是"receiver"。
当第二个人回复时,他就是"speaker",第一个收到回复的人就是"receiver"。
此查询适用于 XAMPP(其中 ONLY_FULL_GROUP_BY 可能已禁用):
SELECT sq.* FROM (
SELECT speaker, receiver, message, status, date_sent
FROM chat
WHERE speaker = 10001
UNION
SELECT receiver, speaker, message, status, date_sent
FROM chat
WHERE receiver = 10001
ORDER BY date_sent DESC
) AS sq
GROUP BY sq.speaker, sq.receiver
ORDER BY date_sent DESC
如果我有这个示例数据(当我 运行 一个简单的 "SELECT * FROM chat" 查询时生成):
speaker | receiver | message | status | date_sent<br>
10001 | 10001 | how are you? | read | 2018-01-30 07:30:15<br>
10002 | 10001 | fine, thanks | unread | 2018-01-30 07:32:43<br>
10001 | 10003 | hello dear | read | 2018-01-30 07:35:29<br>
10003 | 10001 | hello too! | read | 2018-01-30 07:36:01<br>
10001 | 10004 | hey! | read | 2018-01-30 07:36:38<br>
10001 | 10003 | how are you? | unread | 2018-01-30 07:37:24<br>
我得到这个示例输出(我想要的输出):
speaker | receiver | message | status | date_sent<br>
10002 | 10001 | fine, thanks | unread | 2018-01-30 07:32:43<br>
10001 | 10003 | how are you? | unread | 2018-01-30 07:37:24<br>
10001 | 10004 | hey! | read | 2018-01-30 07:36:38<br>
但是当我 运行 在我的 Ubuntu PC 上查询时(可能启用了 ONLY_FULL_GROUP_BY),我得到这个错误:
Error Code: 1055. Expression #3 of SELECT list is not in GROUP BY
clause and contains nonaggregated column 'sq.message' which is not
functionally dependent on columns in GROUP BY clause; this is
incompatible with sql_mode=only_full_group_by
我可以用来生成所需输出的替代查询是什么?
我可能已经找到答案了。我想我可以在外部查询中做这样的事情:
SELECT speaker, receiver, GROUP_CONCAT(message),
GROUP_CONCAT(date_sent) FROM chat...
在此处得到答案:… -- 由 Bill Karwin 回答。感谢 Frank AK 的尝试。
我有一个 "chat" table 可以记录 2 个人之间的聊天记录。我们称第一人称"speaker",第二人称"receiver"。 当第一个人发送聊天消息时,他将是"speaker"。第二个收到消息的人将是"receiver"。 当第二个人回复时,他就是"speaker",第一个收到回复的人就是"receiver"。
此查询适用于 XAMPP(其中 ONLY_FULL_GROUP_BY 可能已禁用):
SELECT sq.* FROM (
SELECT speaker, receiver, message, status, date_sent
FROM chat
WHERE speaker = 10001
UNION
SELECT receiver, speaker, message, status, date_sent
FROM chat
WHERE receiver = 10001
ORDER BY date_sent DESC
) AS sq
GROUP BY sq.speaker, sq.receiver
ORDER BY date_sent DESC
如果我有这个示例数据(当我 运行 一个简单的 "SELECT * FROM chat" 查询时生成):
speaker | receiver | message | status | date_sent<br>
10001 | 10001 | how are you? | read | 2018-01-30 07:30:15<br>
10002 | 10001 | fine, thanks | unread | 2018-01-30 07:32:43<br>
10001 | 10003 | hello dear | read | 2018-01-30 07:35:29<br>
10003 | 10001 | hello too! | read | 2018-01-30 07:36:01<br>
10001 | 10004 | hey! | read | 2018-01-30 07:36:38<br>
10001 | 10003 | how are you? | unread | 2018-01-30 07:37:24<br>
我得到这个示例输出(我想要的输出):
speaker | receiver | message | status | date_sent<br>
10002 | 10001 | fine, thanks | unread | 2018-01-30 07:32:43<br>
10001 | 10003 | how are you? | unread | 2018-01-30 07:37:24<br>
10001 | 10004 | hey! | read | 2018-01-30 07:36:38<br>
但是当我 运行 在我的 Ubuntu PC 上查询时(可能启用了 ONLY_FULL_GROUP_BY),我得到这个错误:
Error Code: 1055. Expression #3 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'sq.message' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
我可以用来生成所需输出的替代查询是什么?
我可能已经找到答案了。我想我可以在外部查询中做这样的事情:
SELECT speaker, receiver, GROUP_CONCAT(message),
GROUP_CONCAT(date_sent) FROM chat...
在此处得到答案: