fran35
fran35

Reputation: 135

Sql 3 tables (join)

I would like to get the content.id, content.title, content.user and tag (if available), as content.tag.

So far I have tried to query using inner joins but they only show the resulting exc2 (the one with sports tag).

Tables:

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 |
+------------+-------------+

Upvotes: 1

Views: 23

Answers (1)

ScaisEdge
ScaisEdge

Reputation: 133360

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

return only 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

should return all rows with null value then not match

Upvotes: 1

Related Questions