having子句在sql中不起作用

时间:2017-06-01 18:30:51

标签: sql rdbms

我不明白这个陈述中的错误。

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

这是错误消息 HAVING子句(LEN(地址)= 13)没有分组或聚合。

2 个答案:

答案 0 :(得分:4)

addressLEN(address)GROUP BY都不在。{1}}。因此,您需要添加它们或将表达式包装在聚合函数中:

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

或者,如果您只想要长度为13的客户,则可能根本不需要聚合:

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

答案 1 :(得分:1)

尝试使用where子句

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