php mysql选择为未找到WHERE列?
我试图使用SELECTAS中mysql PHP PDO中的WHERE条件,我得到了一个错误php mysql选择为未找到WHERE列?,php,mysql,Php,Mysql,我试图使用SELECTAS中mysql PHP PDO中的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 comp
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 category表,它们应该作为consumer和producer存在于company表中,若consumernull不选择它
下面是上述查询的json结果
如果我删除WHERE条件,我会得到下面的JSON响应
正如您所看到的,某些记录的total_consumers:null不应被选中
你知道如何做到我的观点吗:为什么我不能在WHERE语句中使用SELECT AS
WHERE total_consumers >
or
WHERE total_consumers != null
or
WHERE xx NOT NULL
不能在where子句中使用select中的别名。您必须使用having子句:
不能在where子句中使用select中的别名。您必须使用having子句:
只是澄清一下-您可以使用别名,但不能从聚合列使用别名。WHERE限制了聚合所考虑的行,而HAVING子句是在聚合发生后应用的。我执行了上述操作,第一个SQLSTATE[42000]出现错误:语法错误或访问冲突:1064您的SQL语法有错误;检查与您的MySQL服务器版本对应的手册,以了解第6null行的“GROUP BY category.category_title”附近使用的语法是否正确,以及第二个错误:SQLSTATE[HY000]:一般错误:1111组函数NullAdding的使用无效,组修复后总消费者数>0,非常感谢你让我的一天只是为了澄清-你可以使用别名,但不能从聚合列。WHERE限制了聚合所考虑的行,而HAVING子句是在聚合发生后应用的。我执行了上述操作,第一个SQLSTATE[42000]出现错误:语法错误或访问冲突:1064您的SQL语法有错误;查看与MySQL服务器版本对应的手册,了解第6null行“groupby category.category_title”附近使用的语法是否正确,以及第二个错误:SQLSTATE[HY000]:一般错误:1111无效使用组函数nulladding,在组修复后,消费者总数>0,非常感谢你让我开心
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 )
GROUP BY category.category_title
having total_consumers > 0