【问题标题】:ERROR: column reference "customer" is ambiguous错误:列引用“客户”不明确
【发布时间】:2019-09-13 21:40:55
【问题描述】:

目标

我正在为每个客户(如 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 

【问题讨论】:

  • 两个表都有一个列customerSELECT 语句无法决定您要显示哪一个。所以定义相关表:SELECT one_of_both_tables.customer
  • 并添加GROUP BY one_of_both_tables.customer
  • 很好,谢谢! SELECT tb_get_gap.customer, COUNT(*) hostname FROM tb_get_gap LEFT JOIN tb_get_customers ON tb_get_gap.customer = tb_get_customers.cust_cmdb WHERE tb_get_customers.customer = 'FLEURY' and tb_get_gap.exception = 'NO' GROUP BY tb_get_gap.customer;>

标签: sql postgresql


【解决方案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 进行计数。因此,在这种情况下,您需要按此列进行分组。

【讨论】:

    【解决方案2】:

    您想获得哪个客户专栏? tb_get_gap.customertb_get_customers.customer?

    这就是您收到不明确的引用错误的原因。你需要指出你想要它来自哪个表。

    【讨论】:

    • tb_get_gap.customer = BRASILFOODS 客户 = BRASILFOODS |计数 = 30
    • 将 SELECT 中的 customer 替换为 tb_get_gap.customer
    • SELECT tb_get_gap.customer, COUNT(*) hostname FROM tb_get_gap LEFT JOIN tb_get_customers ON tb_get_gap.customer = tb_get_customers.cust_cmdb WHERE tb_get_customers.customer = 'FLEURY' and tb_get_gap.exception = 'NO' 输出: : 列“tb_get_gap.customer”必须出现在 GROUP BY 子句中或在聚合函数中使用
    猜你喜欢
    • 2015-01-07
    • 2018-07-02
    • 2018-01-26
    • 2012-05-31
    • 2013-05-29
    • 1970-01-01
    • 2020-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多