我想获取 content.id、content.title、content.user 和标签(如果可用)作为 content.tag。
到目前为止,我已经尝试使用内部联接进行查询,但它们只显示生成的 exc2(带有运动标签的那个)。
表格:
content tags
+----+-------+------+ +-------------+
| id | title | user | | id | tag |
+----+-------+------+ +-------------+
| 1 | exc1 | 1 | | 1 | sports |
| 2 | exc2 | 1 | +-------------+
| 3 | exc3 | 2 |
+----+-------+------+
content_tag
+------------+-------------+
| content_id | content_tag |
+------------+-------------+
| 2 | 1 |
+------------+-------------+
最佳答案
select cont.* tags.* from content
inner join content_tag on content.id = content_tag.content_id
inner join tags on content_tag. content_tag = tags.id
只返回exec2
select cont.* from content
left join content_tag on content.id = content_tag.content_id
left join tags on content_tag. content_tag = tags.id
应该返回所有具有空值的行然后不匹配
关于mysql - SQL 3表(连接),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36750443/