Mysql 在where子句中使用联接值
我想对联接表中的值使用where语句。无法理解:Mysql 在where子句中使用联接值,mysql,left-join,Mysql,Left Join,我想对联接表中的值使用where语句。无法理解: SELECT item.*, count(articles.authorid) AS articlecount FROM #_authors AS item LEFT JOIN #_articles AS articles ON (articles.authorid = item.id) WHERE item.published=1 AND articlecount>3 ORDER BY articlecount DESC or
SELECT item.*, count(articles.authorid) AS articlecount
FROM #_authors AS item
LEFT JOIN #_articles AS articles ON (articles.authorid = item.id)
WHERE item.published=1
AND articlecount>3
ORDER BY articlecount DESC
orderby工作正常,但当我添加“AND articlecount>3”时,我得到了错误:“#1054-where子句”中的未知列“articlecount”
什么是包含WHERE on Article Count的最佳方式?提前谢谢 尝试使用
have
按聚合值过滤:
SELECT item.*, count(articles.authorid) AS articlecount
FROM #_authors AS item
LEFT JOIN #_articles AS articles ON (articles.authorid = item.id)
WHERE item.published=1
AND articlecount>3
ORDER BY articlecount DESC
articlecount refers to which table field?
if articlecount is in #_author table then you have to write item.articlecount
or articles.articlecount
WHERE item.published=1
HAVING count(articles.authorid) > 3
ORDER BY articlecount DESC
尝试使用
have
按聚合的valeus进行过滤:
WHERE item.published=1
HAVING count(articles.authorid) > 3
ORDER BY articlecount DESC
对于聚合列,可以使用
HAVING
,如下所示:
SELECT item.*, count(articles.authorid) AS articlecount
FROM #_authors AS item
LEFT JOIN #_articles AS articles ON (articles.authorid = item.id)
WHERE item.published=1
GROUP BY 1,2,3,4,5,6,7,8 -- assuming there are 8 columns in item
HAVING count(articles.authorid) > 3
ORDER BY 9 DESC -- assuming there are 8 columns in item
对于聚合列,可以使用
HAVING
,如下所示:
SELECT item.*, count(articles.authorid) AS articlecount
FROM #_authors AS item
LEFT JOIN #_articles AS articles ON (articles.authorid = item.id)
WHERE item.published=1
GROUP BY 1,2,3,4,5,6,7,8 -- assuming there are 8 columns in item
HAVING count(articles.authorid) > 3
ORDER BY 9 DESC -- assuming there are 8 columns in item
articlecount不在表中,它是语句顶部计数的结果值。该计数在articles表中,但没有名为“articlecount”的数据库字段。这就是问题所在。articlecount不在表中,它是语句顶部计数的结果值。该计数在articles表中,但没有名为“articlecount”的数据库字段。这就是问题所在,它起作用了。在GroupBy之后添加“HAVING count(articles.authord)>3”就成功了!谢谢成功了。在GroupBy之后添加“HAVING count(articles.authord)>3”就成功了!谢谢