user1857116 user1857116 - 2 months ago 8
MySQL Question

Unknown column 'userc.u_id' in 'on clause'

I am using Codeigniter and I am trying to retrieve all users with role ID 3 with all their saved custom fields. For some reason, I get a database error with

"Unknown column 'userc.u_id' in 'on clause'"

$this->db->join('(SELECT GROUP_CONCAT(data) AS custom_data, id AS dataid, u_id
FROM ea_user_cfields userc
GROUP BY id) AS tt', 'userc.u_id = ea.id','left');
$this->db->join('(SELECT GROUP_CONCAT(name) AS custom_name, id AS customid
FROM ea_customfields AS cf
GROUP BY id) AS te', 'userc.c_id = cf.id','left');
$this->db->where('id_roles', $customers_role_id);



return $this->db->get('ea_users ea')->result_array();


Actual query:

SELECT * FROM (`ea_users` ea)

LEFT JOIN (SELECT GROUP_CONCAT(data) AS custom_data, id AS dataid, u_id FROM
ea_user_cfields userc GROUP BY id) AS tt ON `userc`.`u_id` =
`ea`.`id`

LEFT JOIN (SELECT GROUP_CONCAT(name) AS
custom_name, id AS customid FROM ea_customfields AS cf GROUP BY id) AS
te ON `userc`.`c_id` = `cf`.`id` WHERE `id_roles` = '3'

Answer

Use the below code may be it helps you.

SELECT * FROM (`ea_users` ea) 

LEFT JOIN (SELECT GROUP_CONCAT(data) AS custom_data, id AS dataid, u_id FROM
ea_user_cfields userc GROUP BY id) AS tt ON tt.`u_id` =
`ea`.`id` 

LEFT JOIN (SELECT GROUP_CONCAT(name) AS
custom_name, id AS customid FROM ea_customfields AS cf GROUP BY id) AS
te ON tt.`c_id` = `cf`.`id` WHERE `id_roles` = '3'
Comments