简体   繁体   English

具有子句在SQL中不起作用

[英]having clause is not working in sql

I dont understand whats wrong in this statment. 我不明白这句话有什么问题。

  Select customername,LEN(address) 
  FROM customers group by customername having LEN(address) = 13;

This is the error message HAVING clause (LEN(address)=13) without grouping or aggregation. 这是错误消息HAVING子句(LEN(address)= 13),没有分组或聚合。

Neither address nor LEN(address) is in the GROUP BY . addressLEN(address)都不在GROUP BY So, you either need to add them or wrap the expressions in an aggregation function: 因此,您需要添加它们或将表达式包装在聚合函数中:

SELECT customername, MAX(LEN(address))
FROM customers 
GROUP BY customername 
HAVING MAX(LEN(address)) = 13;

Or if you just want customers with a length of 13, perhaps no aggregation is needed at all: 或者,如果您只想要长度为13的客户,则可能根本不需要聚合:

SELECT customername
FROM customers 
WHERE LEN(address) = 13;

Try it with the where clause 尝试使用where子句

  Select customername,LEN(address) as lenadressnamecolumn
  FROM customers where LEN(address) = 13;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM