【问题标题】:Column 'id' in where clause is ambiguouswhere 子句中的列“id”不明确
【发布时间】: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();

怎么了?

【问题讨论】:

    标签: mysql codeigniter mysql-error-1052


    【解决方案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 来自stackoverflow.com/questions/9379644/…的回答
    猜你喜欢
    • 2021-05-30
    • 1970-01-01
    • 1970-01-01
    • 2019-04-05
    • 2017-07-22
    • 1970-01-01
    • 2011-06-11
    • 2019-10-09
    • 1970-01-01
    相关资源
    最近更新 更多