mysql - SQL 3表(连接)

标签 mysql

我想获取 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/

相关文章:

java - hibernate删除多对多关联

php - 通过 PHP 从 MySQL 搜索多种电话号码格式

php - 回显 MySQL 查询的单个结果

MySQL 索引不起作用

c# - 多线程错误 : There is already an open DataReader associated with this Connection which must be closed first

html - 使用 htaccess 处理 DDos?

mysql - 使用游标重复最后返回的行

mysqldump 和分离 INSERT

mysql - 如何在 MYSQL 中合并记录

mysql - 按时间定义的存储过程