phpmysqlcodeigniter

Join Query issue - codeigniter


I have 2 tables tb_user_group and tb_user_follow_group i want to join this table in my style

tb_user_group

enter image description here

tb_user_follow_group

Group Follow Image

i want all records from tb_user_follow_group where tb_user_follow_group.group_id match with tb_user_group.id and is_follow='accept' also include tb_user_group.id = 1 and get all the fields of this two table.

not necessary answer in CodeIgniter Syntax post simple query also

This is my try query

$this->db->select('tb_user_follow_group.*,tb_user_group.*');
$this->db->from('tb_user_group');
$this->db->join('tb_user_follow_group', 'tb_user_group.id = tb_user_follow_group.group_id', 'left inner'); 
$query = $this->db->get();
return $query->result();

Solution

  • Try this untested SQL:

    select ug.*
    from tb_user_group ug
    LEFT JOIN tb_user_follow_group ufg ON ug.id = ufg.group_id
    WHERE ufg.is_follow='accept'
        OR ug.user_id='".$user_id."'
    GROUP BY ug.id;