sql - 错误 : column reference "customer" is ambiguous

标签 sql postgresql

目标

我正在为像 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
  1. 模棱两可”的意思是,有一个可以从多个来源获得的标识符。在这种情况下,您的两个表都包含一个名为 customer 的列。因此,您已通过添加表标识符或别名来指定要显示两者中的哪一个。
  2. 您正在使用聚合函数 (COUNT(*))。您似乎想要按 customer 进行计数。因此,在这种情况下,您需要按此列进行分组。

关于sql - 错误 : column reference "customer" is ambiguous,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57924463/

相关文章:

sql - PostgreSQL 不接受 WHERE 子句中的列别名

postgresql - 使用规则或通知自动刷新物化 View

SQL 删除性能

sql - 将正则表达式应用于 R 中的 SQL 数据库

sql - 报告的错误代码是否被认为是 SQL 注入(inject)?

postgresql - 无法连接到数据库 : Loopback + Heroku + Postgres: ECONNREFUSED 127. 0.0.1:5432

mysql - 如何优化 'IN' 查询?

mysql - 这两个 SQL 查询之间的区别

sql - 使用 to_date() 的 Oracle 查询得到 ORA-01858

mysql - 关系数据库与对象关系数据库