• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
MySQL - Issue with SELECT & GROUP BY [SOLVED]

#1
Hello lovely CI Community,

I have a basic config MySQL 5.7 / PHP 7.0 with this DB : 
  • Pages Table 

  • Tags Table

  • Pages_Tags (Joint Table)
Query : 

Code:
$this->db->from("pages");
$this->db->select("*,GROUP_CONCAT('tags_name')");
$this->db->join('pages_tags', 'pages_tags.pages_id = pages.pages_id', 'left');
$this->db->join('tags', 'tags.tags_id = pages_tags.tags_id', 'left');       
$this->db->group_by("pages.pages_id");

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 Wink 

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 Big Grin 

Thanks a lot !!
Reply

#2
I'm not entirely sure this will work, but try this
PHP Code:
$this->db->select("pages.*,GROUP_CONCAT('tags_name')"); 
Reply

#3
(11-29-2018, 06:49 AM)Pertti Wrote: I'm not entirely sure this will work, but try this
PHP Code:
$this->db->select("pages.*,GROUP_CONCAT('tags_name')"); 

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)
Reply

#4
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");
...
echo 
$row->alltags// instead of $row->{"GROUP_CONCAT('tags_name')"} 
Reply

#5
(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

PHP Code:
$this->db->select("pages.*,GROUP_CONCAT(tags_name) AS alltags");
...
echo 
$row->alltags// instead of $row->{"GROUP_CONCAT('tags_name')"} 

Thanks a lot ! It works  Heart Heart Heart 
You are the best !  Heart Heart Heart
Reply

#6
Now worries, just trying to help people out where I can Smile Glad it worked for you.
Reply

#7
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', ''));"
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.