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
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
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
Share on other sites

Ah yes, I forgot about that bug... came across that one a long time ago while using an ODBC driver using an older version of the connector (not that I can find any refererence to it anymore).  Apparently, it gets confused and "forgets" to stringify it.

Link to comment
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.