如何在full group by中写出下面的简单逻辑?
How to write the following simple logic in full group by?
首先,我不想使用语句
SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
关闭整个组,因为默认情况下它是启用的,我相信这是为了好的目的。
然而,这也让我很头疼。
我在 MySQL 中有一个非常简单的 2 数据库 tables:
聊天和 chat_message。
chat 有很多聊天消息。
聊天有一个名为 status
的列
现在我想在一个 SQL 语句 中找到所有状态为 'active' 的聊天记录 ID,按聊天消息发送日期 排序
因此,我创建了以下SQL:
Select chat_messages.chat_id from chat_messages, chats where chats.status='active' and chat_messages.chat_id=chats.id group by chat_messages.chat_id order by chat_messages.send_date desc
OK,现在当我执行上面的SQL,我得到错误信息:
[Code: 1055, SQL State: 42000] Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'chat_messages.send_date' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
如果我把send_date放入group by,结果只会return多条聊天记录,因为每条聊天table记录有多条聊天消息记录,每条聊天消息记录具有不同的 send_date 值。
OK 现在的问题是:在不关闭 only_full_group_by 的情况下,如何在一个 sql 语句中实现目的?
谢谢。
** 更新 **
不确定为什么 Whosebug 将问题标记为与另一个不必要的帖子重复。它与那个问题明显不同,因为这个问题试图解决 ONLY_FULL_GROUP_BY 打开时的错误消息。
在引入完整的分组依据之前,查询过去非常简单,但现在开始出错。我希望该网站知道没有适合所有人的尺寸,并停止在过去使用过时的解决方案来欺骗新问题,因为它无济于事,只会误导他人。
I want to find all the chat record id that has status='active', order by it's chat messages send date in one SQL statement
我了解到您想按最新消息对聊天进行排序。如果是这样,你可以这样做:
select c.*
from chat c
where c.status = 'active'
order by (select max(cm.send_date) from chat_message cm where cm.chat_id = c.id)
如果还想显示最后一条消息的日期,我们可以将关联子查询移到select
子句中:
select c.*,
(select max(cm.send_date) from chat_message cm where cm.chat_id = c.id) last_send_date
from chat c
where c.status = 'active'
order by last_send_date
如果你只需要聊天的id(不是其他聊天主数据),你甚至不需要看聊天table:
select chat_id
from chat_message
group by chat_message
order by max(send_date)
首先,我不想使用语句
SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
关闭整个组,因为默认情况下它是启用的,我相信这是为了好的目的。 然而,这也让我很头疼。
我在 MySQL 中有一个非常简单的 2 数据库 tables:
聊天和 chat_message。 chat 有很多聊天消息。 聊天有一个名为 status
的列现在我想在一个 SQL 语句 中找到所有状态为 'active' 的聊天记录 ID,按聊天消息发送日期 排序
因此,我创建了以下SQL:
Select chat_messages.chat_id from chat_messages, chats where chats.status='active' and chat_messages.chat_id=chats.id group by chat_messages.chat_id order by chat_messages.send_date desc
OK,现在当我执行上面的SQL,我得到错误信息:
[Code: 1055, SQL State: 42000] Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'chat_messages.send_date' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
如果我把send_date放入group by,结果只会return多条聊天记录,因为每条聊天table记录有多条聊天消息记录,每条聊天消息记录具有不同的 send_date 值。
OK 现在的问题是:在不关闭 only_full_group_by 的情况下,如何在一个 sql 语句中实现目的?
谢谢。
** 更新 **
不确定为什么 Whosebug 将问题标记为与另一个不必要的帖子重复。它与那个问题明显不同,因为这个问题试图解决 ONLY_FULL_GROUP_BY 打开时的错误消息。
在引入完整的分组依据之前,查询过去非常简单,但现在开始出错。我希望该网站知道没有适合所有人的尺寸,并停止在过去使用过时的解决方案来欺骗新问题,因为它无济于事,只会误导他人。
I want to find all the chat record id that has status='active', order by it's chat messages send date in one SQL statement
我了解到您想按最新消息对聊天进行排序。如果是这样,你可以这样做:
select c.*
from chat c
where c.status = 'active'
order by (select max(cm.send_date) from chat_message cm where cm.chat_id = c.id)
如果还想显示最后一条消息的日期,我们可以将关联子查询移到select
子句中:
select c.*,
(select max(cm.send_date) from chat_message cm where cm.chat_id = c.id) last_send_date
from chat c
where c.status = 'active'
order by last_send_date
如果你只需要聊天的id(不是其他聊天主数据),你甚至不需要看聊天table:
select chat_id
from chat_message
group by chat_message
order by max(send_date)