where 子句中的列“id”不明确

Posted

技术标签:

【中文标题】where 子句中的列“id”不明确【英文标题】:Column 'id' in where clause is ambiguous 【发布时间】:2014-05-03 04:04:17 【问题描述】:

我收到此错误,但我不知道为什么?

错误号:1052 where 子句中的列 'id' 不明确

SELECT `leads`.*,
       `customers`.`id` AS customers_id,
       `customers`.`name` AS customers_name,
       `customers`.`company` AS customers_company,
       `customers`.`email` AS customers_email,
       `customers`.`phone` AS customers_phone,
       `customers`.`created_at` AS customers_created_at,
       `customers`.`updated_at` AS customers_updated_at,
       `customers`.`ip_address` AS customers_ip_addressFROM (`leads`)
JOIN `customers` ON `customers`.`id` = `leads`.`customer_id`
WHERE `id` = '3'
  AND `leads`.`id` = '1'LIMIT 1

文件名:/home/www/REMOVED/models/lead.php

行号:12

函数如下所示:

function get($id)

  $this->db->select('leads.*, customers.id AS customers_id, customers.name AS customers_name, customers.company AS customers_company, customers.email AS customers_email, customers.phone AS customers_phone, customers.created_at AS customers_created_at, customers.updated_at AS customers_updated_at, customers.ip_address AS customers_ip_address');
  $this->db->where('leads.id', '1');
  $this->db->from('leads');
  $this->db->join('customers', 'customers.id = leads.customer_id');
  $this->db->limit(1);
  $query = $this->db->get();

  if ($query->num_rows() == 1)
  
    $result = $query->result();
    return $result[0];
  

第 12 行是$query = $this->db->get();

怎么了?

【问题讨论】:

【参考方案1】:
WHERE id = '3'

您没有指定 id 字段来自哪个表。你的意思是:

WHERE customer.id = '3'

条件将是 customer.id 而不是 id

【讨论】:

我在我的代码中指定了它? $this->db->join('customers', 'customers.id = Leads.customer_id'); 这就是 join 子句 - 查看 SQL 示例中的 WHERE 子句 - 就在那里 但是在我的代码中我确实指定了表格? $this->db->where('leads.id', '1'); 是的,但正如@Geoff Williams 所说,您的代码中似乎有一个WHERE id = '3',因此,您的代码中可能会有一个$this->db->where('id', 3),它正在添加WHERE给你错误 另外,检查@Mikhail 来自***.com/questions/9379644/…的回答

以上是关于where 子句中的列“id”不明确的主要内容,如果未能解决你的问题,请参考以下文章

SQLSTATE [23000]:违反完整性约束:1052 where 子句中的列 'tenant_id' 不明确

当列不明确时,有啥方法可以强制 Mysql 在 where 子句中使用 select 中的列?

带有限定标识符的 where 子句中的不明确列

Laravel关系列'id'在where子句中不明确

如何修复程序中的“歧义”where子句

Laravel Eloquent:违反完整性约束:1052 列“id”在 where 子句不明确