Inner-join for 3 tables
suggest changeAssume three tables for a site that tags posts.
- The first table holds posts.
- The second holds tags.
- The third relates tags to posts.
First table, videogame:
id | title | reg_date | Content |
—— | —— | —— | —–– |
1 | BioShock Infinite | 2016-08-08 | ….
“tags” table
id | name |
— | ——— |
1 | yennefer |
2 | elizabeth |
“tags_meta” table
post_id | tag_id |
—– | —– | 1 | 2 |
SELECT videogame.id,
videogame.title,
videogame.reg_date,
tags.name,
tags_meta.post_id
FROM tags_meta
INNER JOIN videogame ON videogame.id = tags_meta.post_id
INNER JOIN tags ON tags.id = tags_meta.tag_id
WHERE tags.name = "elizabeth"
ORDER BY videogame.reg_date
This returns posts related to the tag elizabeth. The sample row links post 1 to tag 2, so the result is BioShock Infinite.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents