2014-03-25 40 views
5

我得到這個錯誤,我想不通爲什麼?where子句中的列'id'含糊

錯誤編號:1052
列 'ID' 在where子句是曖昧

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

T他的功能是這樣的:

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();

有什麼不對?

回答

11
WHERE id = '3' 

您不指定id字段來自哪個表。您的意思是:

WHERE customer.id = '3' 
+0

我指定它在我的代碼? $ this-> db-> join('customers','customers.id = leads.customer_id'); – Casperlarsen

+0

多數民衆贊成在加入條款 - 看你的SQL示例中的WHERE子句 - 它的權利 –

+0

但在我的代碼中,我指定了表? $ this-> db-> where('leads.id','1'); – Casperlarsen