MIN COUNT上的SQL错误代码1248

时间:2015-11-01 19:59:22

标签: mysql sql

在MySQL数据库中,我有一个名为'customer'的表。任务是列出客户数量最少的州名称,而不显示没有(零)客户的州。

这是我的命令:

SELECT MIN(mycount)  
(SELECT
  state,COUNT(customerNumber) mycount
FROM customers
WHERE state IS NOT NULL
GROUP BY customers.state
HAVING COUNT(customerNumber) > 0);

但它会出现#1248 - Every derived table must have its own alias

错误

2 个答案:

答案 0 :(得分:2)

您错过了from条款。此外,正如错误所示,您必须为派生表(t)指定别名,如下所示。它也可以是其他任何东西。)

SELECT MIN(mycount)  
from
(SELECT state,COUNT(customerNumber) mycount
FROM customers
WHERE state IS NOT NULL
GROUP BY customers.state
HAVING COUNT(customerNumber) > 0) t --table alias

答案 1 :(得分:0)

如果您希望拥有最少客户的州,则查询应如下所示

count(*)

如果您想要计算,也可以将select添加到外部tr