在codeigniter中的内部联接

this is the error...

A Database Error Occurred

Error Number: 23000/1052

Column 'id' in field list is ambiguous

SELECT id FROM tbl_vendor INNER JOIN tbl_item ON id=vendor_id WHERE shop = 'BVC'

Filename: C:/xampp/htdocs/parts/system/database/DB_driver.php

Line Number: 691

$shop= $this->input->post('vendor');
                      $this->db->select('id');
                      $this->db->from('tbl_vendor');
                      $this->db->join('tbl_item', 'vendor_id=id', 'inner');
                      $this->db->where('shop', $shop);
                      $query=$this->db->get();

Because you have to columns the same try something like this

$shop = $this->input->post('vendor');

$query = $this->db->select('i.id, v.id as vender_id')
                  ->from('tbl_vendor v')
                  ->join('tbl_item i', 'i.id = v.id')
                  ->where('v.shop', $shop)
                  ->get();

return $query->row_array();

Note:

  • row_array() will return single item
  • result_array() multiple items

https://www.codeigniter.com/user_guide/database/results.html

The error you receive means the field name "id" exists in one or more of the tables you are querying, can you please write which columns you have in each table? Giving a table an alias and using it together with your field will solve your problem: For example tbl_vendor.id

Just add the table name as ambiguous like below:

$shop= $this->input->post('vendor');
                      $this->db->select('tbl_vendor.id');
                      $this->db->from('tbl_vendor');
                      $this->db->join('tbl_item', 'tbl_item.id=tbl_vendor.id','inner');
                      $this->db->where('tbl_vendor.shop', $shop);
                      $query=$this->db->get();
                      $data=$query->result_array();

so that query will be SELECT tbl_vendor.id FROM tbl_vendor INNER JOIN tbl_item ON tbl_vendor.id=tbl_item.id WHERE tbl_vendor.shop = 'BVC'

More information check here