gpt4 book ai didi

python - 查询以选择帖子和所有附件

转载 作者:行者123 更新时间:2023-11-30 00:17:43 25 4
gpt4 key购买 nike

所以,我正在建立这个论坛。我在编写优雅的选择查询来从数据库获取特定主题的所有帖子时遇到问题。这是架构:

表格帖子具有以下结构:

   id int(5) not null auto_increment,
user_id int(5) not null,
topic_id int(5) not null,
post_date datetime not null,

现在,还有一个名为附件的表,其中引用帖子附件。我希望能够仅通过一个查询来选择特定帖子的附件。这是附件表结构:

   id int(5) not null auto_increment,
post_id int(5) not null,
post_name varchar(255) not null default '',
path varchar(255) not null,

这不是我的 table 的实际样子。我只是想要一个基本的想法如何去做。我什至想不出最起码的解决办法。现在附件表有一个引用帖子表的外键。但是,一篇文章可能有很多附件。那么如何在一次查询中选择一篇文章及其所有附件呢?也许子查询可以工作?

最佳答案

SELECT * FROM attachments WHERE post_id in (SELECT id FROM posts WHERE topic_id = ?);

类似这样的吗?

编辑:毕竟不是海报想要的。我认为在不使用多个查询的情况下不可能对表结构执行您想要的操作。

关于python - 查询以选择帖子和所有附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23526204/

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