目标
我正在为像 BRASILFOODS 这样的每个客户在主机名列上执行一个计数器,我想要的一件事是并排显示 BRASILFOODS 计数。
查询
SELECT COUNT(*) hostname, customer
FROM tb_get_gap
LEFT JOIN tb_get_customers
ON tb_get_gap.customer = tb_get_customers.cust_cmdb WHERE tb_get_customers.customer = 'BRASILFOODS' and tb_get_gap.exception = 'NO';
输出
>[Error] Script lines: 1-4 --------------------------
ERROR: column reference "customer" is ambiguous
Line: 1
最佳答案
SELECT
tgg.customer, -- 1
COUNT(*)
FROM tb_get_gap tgg
LEFT JOIN tb_get_customers tgc
ON tgg.customer = tgc.cust_cmdb
WHERE tgc.customer = 'BRASILFOODS'
AND tgg.exception = 'NO'
GROUP BY tgg.customer -- 2
- “模棱两可”的意思是,有一个可以从多个来源获得的标识符。在这种情况下,您的两个表都包含一个名为
customer
的列。因此,您已通过添加表标识符或别名来指定要显示两者中的哪一个。 - 您正在使用聚合函数 (
COUNT(*)
)。您似乎想要按customer
进行计数。因此,在这种情况下,您需要按此列进行分组。
关于sql - 错误 : column reference "customer" is ambiguous,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57924463/