在where子句中,列'id'不明确。

15

我遇到了这个错误,但是我不知道为什么会出现?

Error Number: 1052
Column 'id' in where clause is ambiguous

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个回答

32
WHERE id = '3'

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

WHERE customer.id = '3'

条件将使用customer.id而不是id


我在代码中指定它吗?$this->db->join('customers', 'customers.id = leads.customer_id'); - Casperlarsen
那是连接子句 - 看看你 SQL 示例中的 WHERE 子句 - 就在那里 - Geoff Williams
但是在我的代码中,我确实指定了表格吗?$this->db->where('leads.id', '1'); - Casperlarsen
是的,但正如@Geoff Williams所说,你的代码中似乎有一个WHERE id ='3',所以你可能在代码中的某个地方有一个$this->db->where('id', 3),它正在添加WHERE并导致错误。 - Federico J.
另外,请查看@Mikhail在http://stackoverflow.com/questions/9379644/codigniter-active-record-update-query-taking-old-where-clause的答案。 - Federico J.
这节省了我的时间 :) - Bhargav Nanekalva

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接