Stats

Contributors: 1 Sunday, September 11, 2016
Licensed under: CC-BY-SA
Not affiliated with Stack Overflow
Rip Tutorial: riptutorial@gmail.com
Roadmap: roadmap

Inner-join for 3 tables

Example

let's assume we have three table which can be used for simple website with Tags.

  • Fist table is for Posts.
  • Second for Tags
  • Third for Tags & Post relation

fist table "videogame"

idtitlereg_dateContent
1BioShock Infinite2016-08-08....

"tags" table

idname
1yennefer
2elizabeth

"tags_meta" table

post_idtag_id
12
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 code can return all posts which related to that tag "#elizabeth"