聚合我也在 MySQL 的 where 子句中使用的连接结果

发布于 2024-12-11 09:30:47 字数 584 浏览 5 评论 0原文

我引用简单机器论坛网络论坛软件中的两个表:smf_members 和 smf_members。 smf_members 中的每一行都有一个字段:id_group,我对其中的值感兴趣:1,9,20,26,23,27(使用 IN() 子句)。一般目标是确定 smf_members 表中上述组 ID 中的哪些行在 90 天内没有在 smf_messages 中输入行。 smf_messages 中的 poster_time 是 unix 时间戳。到目前为止我所得到的是:

SELECT
  m.id_member,
  m.id_group,
  from_unixtime(max(ms.poster_time))
FROM
  smf_members m
  LEFT JOIN smf_messages ms
    USING(id_member)
WHERE
  max(ms.poster_time) < (NOW() < (86400 * 90)
GROUP BY
  m.id_member

它失败并出现错误 1111:组函数的使用无效,因为我在 where 子句中使用 max() 。如何聚合我的加入结果以仅引用基于 poster_time 字段的最新条目?

I'm referencing two tables in the simple machines forum web forum software: smf_members, and smf_members. each row in smf_members has a field: id_group, of which I am interested in values: 1,9,20,26,23,27 (using the IN() clause). The general goal is to determine which rows in the smf_members table that are in the above group id's haven't had a row entered in smf_messages in 90 days. poster_time in smf_messages is a unix timestamp. What I have so far is:

SELECT
  m.id_member,
  m.id_group,
  from_unixtime(max(ms.poster_time))
FROM
  smf_members m
  LEFT JOIN smf_messages ms
    USING(id_member)
WHERE
  max(ms.poster_time) < (NOW() < (86400 * 90)
GROUP BY
  m.id_member

It fails with and ERROR 1111: Invalid use of the group function, since I am using max() in the where clause. How can I aggregate my join results to only reference the latest entry based off the poster_time field?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独﹏钓一江月 2024-12-18 09:30:47

有几种方法可以做到这一点。

  1. 从成员中选择 *
    WHERE id_member IN (...)
    并且不存在
    ( 从消息中选择 1
    WHERE is_member = Members.id_member
    并且海报_时间> (UNIX_TIMESTAMP*() - 90 * 24 * 60 * 60)

  2. 查找外连接未找到任何内容的情况
    选择不同的 id_member
    来自会员作为米...
    LEFT JOIN 消息 AS msg USING ....
    WHERE msg.id IS NULL

  3. SELECT * FROM 成员
    id_member 所在位置 ( .... )
    并且 id_member 不在
    (从消息中选择 id_member
    哪里海报_时间> (UNIX_TIMESTAMP*() - 90 * 24 * 60 * 60)
    )

There are several ways to this.

  1. SELECT * FROM members
    WHERE id_member IN ( ... )
    AND NOT EXISTS
    ( SELECT 1 FROM messages
    WHERE is_member = members.id_member
    AND poster_time > (UNIX_TIMESTAMP*() - 90 * 24 * 60 * 60)

  2. look for the cases where an outer join finds nothing
    SELECT DISTINCT id_member
    FROM MEMBERS AS m ...
    LEFT JOIN messages AS msg USING ....
    WHERE msg.id IS NULL

  3. SELECT * FROM members
    WHERE id_member IN ( .... )
    AND id_member NOT IN
    (SELECT id_member FROM messages
    WHERE poster_time > (UNIX_TIMESTAMP*() - 90 * 24 * 60 * 60)
    )

我做我的改变 2024-12-18 09:30:47

在尝试 GROUP BY 之前,您可能需要尝试使用临时表来计算 max(ms.poster_time)。像这样的东西应该有效。

SELECT * FROM 
(SELECT
  m.id_member,
  m.id_group,
  from_unixtime(max(ms.poster_time))
FROM
  smf_members m
  LEFT JOIN smf_messages ms
    USING(id_member)
WHERE
  max(ms.poster_time) < (NOW() < (86400 * 90) ) AS tmp

GROUP BY
  m.id_member

通过这种方式,您可以将聚合函数与 GROUP BY 结合使用。如果您有大型数据集,它的性能不会很好。如果是这种情况,您可能需要考虑定期汇总内表生成的数据,然后对其进行查询。

You may want to try using a temporary table to figure out the max(ms.poster_time) before attempting the GROUP BY. Something like this ought to work.

SELECT * FROM 
(SELECT
  m.id_member,
  m.id_group,
  from_unixtime(max(ms.poster_time))
FROM
  smf_members m
  LEFT JOIN smf_messages ms
    USING(id_member)
WHERE
  max(ms.poster_time) < (NOW() < (86400 * 90) ) AS tmp

GROUP BY
  m.id_member

Doing it this way allows you to use the aggregate function with the GROUP BY. It won't perform very well if you have large datasets. If that's the case, you may want to consider regularly summarizing the data generated by the inner table, and then querying on that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文