just having an issue with a mySQL
query. Basically I have three tables
- tag
- directorycolumn
- directorycolumntags
There is a many to many
relationship between directorycolumn and tag hence the directorycolumntags table acting as the junction table. I am trying to construct a query that will return the tag name from the tag table using a left join
between the other two tables.
This is my Query:
SELECT t.name FROM tag t
LEFT JOIN directorycolumntags dct
ON t.id = dct.tag_id
AND dct.directorycolumn_id = '178'
But rather than just returning the tags associated with columns with the id 178 it returns all the tags.Can anyone help me on this?
This is the data in the directorycolumntags
table:
id directorycolumn_id tag_id description created
29 178 1 \N 2014-11-05 17:31:22
30 178 2 \N 2014-11-05 17:31:30
31 178 3 \N 2014-11-05 17:31:42
This is the data in the tag
table:
id name description
31 Tag 1 This is Tag 1
32 Tag 2 This is Tag 2
33 Tag 3 This is Tag 3
34 Tag 4 This is Tag 4
35 Tag 5 This is Tag 5
This is the result of my above query:
name
Tag 1
Tag 2
Tag 3
Tag 4
Tag 5
If I change the query to an INNER JOIN
Like so:
SELECT t.name FROM tag t
JOIN directorycolumntags dct
ON t.id = dct.tag_id
AND dct.directorycolumn_id = '178'
I am returned no results. This is strange as when I execute this query
SELECT * FROM directorycolumntags WHERE directorycolumn_id = 178
I get the following results:
id directorycolumn_id tag_id description created
29 178 1 \N 2014-11-05 17:31:22
30 178 2 \N 2014-11-05 17:31:30
31 178 3 \N 2014-11-05 17:31:42