Jump to content

GROUP_CONCAT


kirk112

Recommended Posts

Hi,

 

I am trying to group fields together, which I have managed using GROUP_CONCAT, but when I concat on a SMALLINT datatype the results is return as

 

ls_id  locations 

1      [bLOB - 2 Bytes]

2      [bLOB - 2 Bytes]

 

instead of

 

ls_id  locations 

1      '1', '2'

2      '1', '3'

 

This is the query that I have been using.

 

SELECT ls_id, GROUP_CONCAT(DISTINCT(ls_id), '\''  SEPARATOR ',\'') AS locations FROM ip_locations AS ip

LEFT JOIN location_specific AS ls ON ls.specific_id = ip.ls_id

WHERE ip.ip_code IN ('A1', 'A2', 'A3', 'A4') GROUP BY ls_id

 

Does anyone know where I am going wrong?

 

Thanks for your help!

Link to comment
https://forums.phpfreaks.com/topic/71521-group_concat/
Share on other sites

Hi Barand I have removed the GROUP BY ls_id and I am still getting the same incorrect results

 

I have been running this query in phpMyAdmin.

 

If I run this query and replace GROUP_CONCAT(ls_id) to a varchar field (instead of an smallint) it run correctly, does anyone have any idea on where to start debugging this I have been going around in circles for days

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/71521-group_concat/#findComment-360402
Share on other sites

Don't you want to group locations for each ls_id?

 

SELECT ls.ls_id, GROUP_CONCAT(DISTINCT ip.location SEPARATOR ',') as locations

FROM ip_locations AS ip

LEFT JOIN location_specific AS ls ON ls.specific_id = ip.ls_id

WHERE ip.ip_code IN ('A1', 'A2', 'A3', 'A4') GROUP BY ls.ls_id

Link to comment
https://forums.phpfreaks.com/topic/71521-group_concat/#findComment-360415
Share on other sites

No idea why this works but if I change the original query to

 

SELECT ls_id, CONCAT('\'', GROUP_CONCAT(DISTINCT(ls_id), '\'' SEPARATOR ',\'')) AS locations FROM ip_locations AS ip

LEFT JOIN location_specific AS ls ON ls.specific_id = ip.ls_id

WHERE ip.ip_code IN ('A1', 'A2', 'A3', 'A4') GROUP BY ls_id

 

it works perfect, but a soon as I remove the first concat, it reverts back to giving me all the results as a blob.

 

Any ideas why??

 

Thanks Barand for your help, got myself mixed up with the fields that I wanted to select, should be able to work it out after I have gotten some sleep.

 

Thanks again everyone!

Link to comment
https://forums.phpfreaks.com/topic/71521-group_concat/#findComment-360433
Share on other sites

Hi Fenway, thanks for your response explained a lot.

 

Just one more thing when using group_concat it always retrieves one row even if the where does not match any fields, I can over come this by using 'having length'  but this seems a little bit messy, just wondering if there is a better way to achieve this?

 

SELECT CONCAT('\'',GROUP_CONCAT(DISTINCT(ls_id), '\''  SEPARATOR ',\'')) AS locations FROM ip_locations AS ip WHERE ip.ip_code = 'jkj' HAVING length(locations) > 0

 

 

Again thanks for your time

Link to comment
https://forums.phpfreaks.com/topic/71521-group_concat/#findComment-360832
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.