1
我试图用WHERE
条件在MySQL PHP
PDO
从SELECT AS
,我得到了错误PHP MySQL选择与WHERE列未找到?
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'total_consumers' in 'where clause'null
我的查询:
SELECT category.* , SUM(Consumer.capacity) AS total_consumers
FROM company AS company
RIGHT JOIN company AS Consumer ON (Consumer.category_id = company.category_id AND Consumer.company_type = 'Consumer' )
RIGHT JOIN category AS category ON (category.category_id = company.category_id )
WHERE total_consumers > 0
GROUP BY category.category_title
目标:
我想所有记录Inc分类表,它们应该作为消费者和生产者存在于公司表中,如果消费者null不选择它
这里是上面的查询
的JSON结果如果我删除WHERE
条件我得到了以下JSON响应
就像你看到的一些记录有total_consumers : null
不应该选择
任何想法如何做我的观点:(为什么我不能在WHERE语句中使用SELECT AS)
WHERE total_consumers >
or
WHERE total_consumers != null
or
WHERE xx NOT NULL
只是为了澄清 - 您可以使用别名,但不能使用聚合列。 WHERE限制考虑进行聚合的行,而HAVING子句在聚合发生后应用。 – MatsLindh
我做了上面的事,我第一个错误'SQLSTATE [42000]:语法错误或访问冲突:1064您的SQL语法错误;检查与您的MySQL服务器版本相对应的手册,以在第6null行'GROUP BY category.category_title'附近使用正确的语法,并检查第二个错误:SQLSTATE [HY000]:常规错误:1111组函数无效' – Jack
@NinjaDevelopers请参阅我的更新回答 – Jens