我有以下查询,它使用引用表tags_titles和tags_blogs来与包含标签的表进行比较,称为tags。标签本身保存在t.label列中。
我的问题是有时它count() 过多的 total_matches。通常在tags_titles 和tags_blogs 中可以找到引用的标签。有没有办法使内部连接互斥,或其他解决方案,以便与列 t.label 的实际匹配计数准确?
SELECT b.blog_id AS id, b.title AS title, b.body AS body, COUNT(t.label) AS total_matches, b.creation_time AS creation_time, '1' AS type
FROM tags AS t
INNER JOIN tags_titles AS tt
ON tt.tag_id = t.tag_id
INNER JOIN tags_blogs AS tb
ON tb.tag_id = t.tag_id
INNER JOIN blogs AS b
ON tt.blog_id=b.blog_id OR tb.blog_id=b.blog_id
WHERE t.label IN ($in) AND b.title IS NOT NULL
GROUP BY id, title, body, creation_time, type