gpt4 book ai didi

mysql - 获取与同一个表中的另一个属性匹配的所有行

转载 作者:行者123 更新时间:2023-11-29 12:29:56 24 4
gpt4 key购买 nike

我有这个表(称为posts):

+---------+----------+---------+
| post_id | reply_to | user_id |
+---------+----------+---------+
| 1 | 0 | 1 |
| 2 | 0 | 1 |
| 3 | 2 | 2 |
| 4 | 1 | 3 |
| 5 | 3 | 2 |
+---------+----------+---------+

如何选择对 user_id = 1 帖子进行回复的所有帖子?

生成的表格应如下所示:

+---------+----------+---------+
| post_id | reply_to | user_id |
+---------+----------+---------+
| 3 | 2 | 2 |
| 4 | 1 | 3 |
+---------+----------+---------+

最佳答案

这是快速变体:

select *
from posts
where reply_to in (select post_id from posts where user_id = 1);

因此,内部查询选择用户的所有帖子,外部查询选择对这些帖子的所有直接回复

关于mysql - 获取与同一个表中的另一个属性匹配的所有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27713851/

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