【发布时间】:2019-05-05 13:16:13
【问题描述】:
我有一个名为 posts 的表和另一个名为 attachments 的表...
一个post 可以有多个attachments...
所以,我创建了一个名为post_attachment..的中间表。
如何在A SINGLE QUERY中获取包含attachments数据的posts数据列表?
..
请参考以下内容,以便更清楚地理解...
表结构如下:
-
posts 表有 4 列:
- 身份证
- 标题
- body_text
-
attachments 表有 3 列:
- 身份证
- 文件名
- file_url
-
post_attachment 表有 2 列:
- post_id
- file_id
以下是具有多个attachments 的post 示例
posts表:
-
attachments表:
-
post_-attachment表:
在上面的示例中,它告诉post(ID:1)有 3 个attachments,即 ID:1,2 和 3。
所以,问题是如何获取在单一查询中包含posts.title、attachments.filename、attachments.file_url 列的帖子列表?
【问题讨论】:
标签: mysql one-to-many