MySQL - Issue with SELECT & GROUP BY [SOLVED] - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: MySQL - Issue with SELECT & GROUP BY [SOLVED] (/showthread.php?tid=72276) |
MySQL - Issue with SELECT & GROUP BY [SOLVED] - Coool6 - 11-29-2018 Hello lovely CI Community, I have a basic config MySQL 5.7 / PHP 7.0 with this DB :
Code: $this->db->from("pages"); and i have the following error : Code: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column this is incompatible with sql_mode=only_full_group_by So i made a lot of researches, and seems there are 2 solutions to fix it (more info here : https://dev.mysql.com/doc/refman/5.7/en/group-by-handling.html) : 1/ To change the SQL mode ===> I don't want to change the default config 2/ To have the same column in the SELECT & GROUP BY ===> I need a lot of columns in the SELECT and not only the ones in GROUP BY. Especially if the query is more complicated with a lot more LEFT JOIN. I need to display all theses infos. Do you have any other solutions ? Alternative with other methods ? I'm open to anything What i need is to get the list of pages and for each page the tags linked to, in one query. It seems so easy but it doesn't work Thanks a lot !! RE: MySQL - Issue with SELECT & GROUP BY - Pertti - 11-29-2018 I'm not entirely sure this will work, but try this PHP Code: $this->db->select("pages.*,GROUP_CONCAT('tags_name')"); RE: MySQL - Issue with SELECT & GROUP BY - Coool6 - 11-29-2018 (11-29-2018, 06:49 AM)Pertti Wrote: I'm not entirely sure this will work, but try this Thanks for your fast reply ! So i tried your solution and it works, BUT when i echo the GROUP_CONCAT('tags_name'), it only displays "tags_name" or "tags_name,tags_name", the value is the field's name and NOT the value itself... Weird. So instead of giving : "environment,technology,economy" it gives "tags_name,tags_name,tags_name" Any idea how to fix it ? I tried to add "themes.*" in the SELECT, but it gives the same error as before (Expression #2 of SELECT bla bla) RE: MySQL - Issue with SELECT & GROUP BY - Pertti - 11-29-2018 Ah right, I see what's happened here, I thought I copied your original statement, basically it always uses tags_name as string... just pop off the quotes and it should work hopefully. Also you can add AS to name it nicer PHP Code: $this->db->select("pages.*,GROUP_CONCAT(tags_name) AS alltags"); RE: MySQL - Issue with SELECT & GROUP BY - Coool6 - 11-29-2018 (11-29-2018, 07:29 AM)Pertti Wrote: Ah right, I see what's happened here, I thought I copied your original statement, basically it always uses tags_name as string... just pop off the quotes and it should work hopefully. Also you can add AS to name it nicer Thanks a lot ! It works You are the best ! RE: MySQL - Issue with SELECT & GROUP BY - Pertti - 11-29-2018 Now worries, just trying to help people out where I can Glad it worked for you. RE: MySQL - Issue with SELECT & GROUP BY [SOLVED] - Ettore - 11-29-2018 Don't know if useful, but for a quick fix I found this hint: https://simplerisk.freshdesk.com/support/solutions/articles/6000164283 PHP Code: $this->db->query("SET sql_mode=(SELECT REPLACE(@@sql_mode, 'ONLY_FULL_GROUP_BY', ''));") RE: MySQL - Issue with SELECT & GROUP BY [SOLVED] - creatiko - 03-19-2020 (11-29-2018, 01:28 PM)Ettore Wrote: Don't know if useful, but for a quick fix I found this hint: This is the answer that helped me! RE: MySQL - Issue with SELECT & GROUP BY [SOLVED] - inumaru - 06-07-2021 Use ANY_VALUE(tags.table_name) for all tags table in your select query. |