查询外部联接问题。
我有这三张桌子:
表文章(
ID,
articletitle,articletext)
- 等等。其他栏目在这里无所谓
表类别(
ID,
名)
表articlecategory(
条款ArticleID,
类别ID,
index articleid,categoryid)
当我执行以下查询时,我会为任何具有多个类别的文章获得重复项
查询:
SELECT category.name AS categoryname
, article.id as articleid, article.articletitle AS articletitle, article.articletext as articletext, article.articlev as articlev, article.visible as visible
, GROUP_CONCAT(othercategory.name) AS othercategories
FROM category
LEFT OUTER
JOIN articlecategory
ON articlecategory.categoryid = category.id
LEFT OUTER
JOIN article
ON article.id = articlecategory.articleid
LEFT OUTER
JOIN articlecategory AS otherarticlecategory
ON otherarticlecategory.articleid = articlecategory.articleid
LEFT OUTER
JOIN category AS othercategory
ON othercategory.id = otherarticlecategory.categoryid
GROUP
BY category.name ASC
, article.articletitle ASC';
结果是这样的:
foreach
article 1
othercategory = category(s) finishing, sanding
article 1
category(s) finishing, sanding
article2
category(s) tips
article 3
category(s) tips, informational
article 3
category(s) tips, informational
endforeach
我没有写这个查询。我猜的那个人已经忙得无法回应。所以我需要一些帮助,但我对提示不够聪明。所以请具体一点。我想我知道问题是什么,但我没有足够的经验来解决它。
答案 0 :(得分:0)
GROUP BY
应该摆脱重复。我还注意到,在您现有的GROUP BY
中,您应该使用别名而不是完全限定名。
尝试将文章ID添加到GROUP BY
。
GROUP BY categoryname ASC, articletitle ASC, articleid ASC