简体   繁体   中英

Inner join in 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();

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

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

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 technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM