gpt4 book ai didi

mysql - 从父值计算子成员

转载 作者:可可西里 更新时间:2023-11-01 07:50:50 26 4
gpt4 key购买 nike

我有一个类似conversationsconversation_timelines 的表格

conversations 表示例

|  id   |   last_active   |   category_id   |
|-------------------------------------------|
| 1 | 1552462134 | 1 |
| 2 | 1552461332 | 1 |
| 3 | 1552462312 | 2 |
| 4 | 1552461772 | 1 |

conversation_timelines 表示例

|  id   |  conversation_id  |  message  |  created_time  |
|--------------------------------------------------------|
| 1 | 1 | hi | 1552462066 |
| 2 | 1 | hello | 1552462172 |
| 3 | 1 | world | 1552462188 |
| 4 | 2 | another | 1552462141 |

我要查询的是统计 conversation_timelines that has created_time > conversation's last_active 中的记录数WHERE conversations.category_id = 1

结果世界是这样的

|  conversation_id  |  count  |
| 1 | 2 |
| 2 | 1 |

最佳答案

使用 joingroup by 如下所示

  SELECT ct.conversation_id,count(ct.message)
FROM conversation_timelines ct
INNER JOIN conversation c on c.id=ct.conversation_id and c.category_id=1
and ct.created_time>c.last_active
GROUP BY ct.conversation_id

关于mysql - 从父值计算子成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55137307/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com