简体   繁体   中英

codeigniter join 3 tables and get data from 1st table where 1st table is linked with 2nd table and 2nd table is linked with 3rd table

user_table(user_id, user_name)

user_group_table(user_id,  group_id)

group_table(group_id, group_name)

I need to retrieve data (user_id, user_name, group_name)

How can I join these tables in CodeIgniter?

  $this->db->query('SELECT user_table.user_id, user_table.user_name,
                    group_table.group_name
                    FROM
                    user_table
                    join user_group_table on user_group_table.user_id=user_table.user_id
                    join group_table on group_table.group_id=user_group_table.group_id')
                    ->result_array();

return it and print/use the array however you like OR you can use Codeigniter Query Builder Class

$where = array(); //if u want to use any condition
$this->db->select('user_table.user_id, user_table.user_name, group_table.group_name');
$this->db->join('user_group_table', 'user_group_table.user_id = user_table.user_id');
$this->db->join('group_table', 'group_table.group_id = user_group_table.group_id');
$result = $this->db->order_by('user_table.user_id', 'desc')->get_where('property', $where)->result_array();   
return $result
$this->db->select('user_table.user_id,user_table.user_name,group_table.group_name');
$this->db->from('user_group_table');
$this->db->join('user_table', 'user_table.user_id = user_group_table.user_id');
$this->db->join('group_table', 'group_table.group_id = user_group_table.group_id');
$query = $this->db->get();
$res = $query->result();
return $res;

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