You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') where 1=1 and users.user_status = 1 group by users.id order by ' at line 13select users.*, membership.id as membership_id, membership.membership_type_id, membership.end_date as membership_end_date, group_concat(user_to_field.field_id) as fields, group_concat(user_to_agegroup.agegroup_id) as agegroups, group_concat(themes_to_users.theme_id) as themes from users left join user_to_field on user_to_field.user_id=users.id left join user_to_agegroup on user_to_agegroup.user_id=users.id left join themes_to_users on themes_to_users.user_id=users.id left join membership on membership.id=(select id from membership where user_id=users.id and membership.status=1 order by id desc limit 1) join users_to_networks on users_to_networks.user_id=users.id and users_to_networks.network_id in () where 1=1 and users.user_status = 1 group by users.id order by users.user_created desc