MySQL - Issue with SELECT & GROUP BY [SOLVED] |
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/...dling.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 !!
I'm not entirely sure this will work, but try this
PHP Code: $this->db->select("pages.*,GROUP_CONCAT('tags_name')"); (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)
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"); (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 !
Now worries, just trying to help people out where I can Glad it worked for you.
Don't know if useful, but for a quick fix I found this hint:
https://simplerisk.freshdesk.com/support...6000164283 PHP Code: $this->db->query("SET sql_mode=(SELECT REPLACE(@@sql_mode, 'ONLY_FULL_GROUP_BY', ''));") (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! |
Welcome Guest, Not a member yet? Register Sign In |