只是遇到了mySQL
查询的问题。基本上我有三张桌子
directorycolumn和tag之间存在many to many
关系,因此directorycolumntags表充当联结表。我正在尝试构建一个查询,该查询将使用其他两个表之间的left join
从标记表返回标记名称。
这是我的查询:
SELECT t.name FROM tag t
LEFT JOIN directorycolumntags dct
ON t.id = dct.tag_id
AND dct.directorycolumn_id = '178'
但是,不是仅仅返回与id为178的列相关联的标签,而是返回所有标签。任何人都可以帮我这个吗?
这是directorycolumntags
表格中的数据:
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
这是tag
表格中的数据:
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
这是我上述查询的结果:
name
Tag 1
Tag 2
Tag 3
Tag 4
Tag 5
如果我将查询更改为INNER JOIN
,请执行以下操作:
SELECT t.name FROM tag t
JOIN directorycolumntags dct
ON t.id = dct.tag_id
AND dct.directorycolumn_id = '178'
我没有结果。当我执行此查询时,这很奇怪
SELECT * FROM directorycolumntags WHERE directorycolumn_id = 178
我得到以下结果:
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
答案 0 :(得分:2)
当您执行LEFT JOIN
时,您将从LEFT表中获取所有行,并从右表中获取不匹配行的NULL值
如果您想要列178的所有标记值,只需使用INNER JOIN
SELECT t.name FROM tag t
JOIN directorycolumntags dct
ON t.id = dct.tag_id
AND dct.directorycolumn_id = '178'
基于对该问题所做的编辑 tag.id和directorycolumntags.tag_id上的连接没有产生任何结果,因为没有匹配。
我认为您需要这样做,以获得预期的结果
SELECT t.name FROM tag t
JOIN directorycolumntags dct
ON concat('Tag ',dct.tag_id) = t.name
AND dct.directorycolumn_id = 178
答案 1 :(得分:1)
您正在使用Left Join
条款将Inner Join
更改为Where
。
将Where
语句移至On
条件。
SELECT t.name FROM tag t
LEFT JOIN directorycolumntags dct
ON t.id = dct.tag_id
AND dct.directorycolumn_id = '178'
编辑:根据@ RADAR的回复,他认为它应该是Inner Join
而不是Outer Join
是正确的。这应该会告诉你你正在寻找什么,不过,应该归功于他:
SELECT t.name FROM tag t
JOIN directorycolumntags dct
ON t.id = dct.tag_id
WHERE dct.directorycolumn_id = '178'