MySQL UNION query separation MySQL UNION query separation codeigniter codeigniter

MySQL UNION query separation


yes you can add another field to select , that you can recognise like (1,2)

$this->db->query('        SELECT 1 as table_id,DISTINCT users.user_pic, users.id, users.username, contacts.accepted         FROM users        LEFT JOIN contacts ON users.id = contacts.user_1        WHERE contacts.user_2 = ' . $this->session->userdata('user_id') . '        UNION DISTINCT        SELECT 2 as table_id,DISTINCT users.user_pic, users.id, users.username, contacts.accepted         FROM users         LEFT JOIN contacts ON users.id = contacts.user_2        WHERE user_1 = ' . $this->session->userdata('user_id'));


I typically use:

$id = $this->session->userdata('user_id');

then use $id in the query. Reads cleaner and on MySQL at least, if the query fails, it lists the value of $id. Never tried calling the session in the query before.