I have planned to use three tables for my sites tagging system, they looks like:
|-----------------------|
|==> photo |
| -> id |
| -> custom_id |
| -> title |
| -> slug |
| -> date |
| -> image_url |
|-----------------------|
|==> tags |
| -> id |
| -> slug |
| -> title |
|-----------------------|
|==> tags_relation |
| -> tid | <-- this is the tags.id
| -> pid | <-- this is the photo.custom_id
|-----------------------|
So, to fetch the recent posts of a specific tag i am using INNER JOIN by the following query:
SELECT p.id, p.custom_id, p.title, p.slug, p.date, p.image_url, t.id, t.slug, tr.*
FROM tags t INNER JOIN
tags_relation tr
ON t.id = tr.tid INNER JOIN
photo p
ON p.custom_id = tr.pid
WHERE t.slug = 'people'
ORDER BY p.date DESC
LIMIT 20
Everything works fine except the fact that the 'slug, id, title' column of the photo table is being replaced by the 'slug, id, title' column of the tags table!
I figured out a solution that is changing the tags columns name, but is there any best practices to solve this problem?
Thanks