Jump to content

mysql group by or concat


jarvis

Recommended Posts

Hi,

I really hope someone can help.

 

I've several tables containing various info. I therefore have to join one table to another.

SELECT 
$table_products.productID,
$table_products.code,
$table_products.name,
$table_products.shortdescription,
$table_products.description,
$table_products.thumbnail,
$table_products.mainimage,
$table_products.extrafield1,
$table_products.extrafield2,
$table_products.extrafield3,
$table_products.price1,
$table_products.weight,
$table_products.scLevel,
$table_extrafields_values.content
FROM $table_products LEFT JOIN $table_extrafields_values
ON $table_products.productID = $table_extrafields_values.productID
AND $table_extrafields_values.extraFieldID=4
WHERE (
   $table_products.productID 
   BETWEEN 2 And 999999) 
AND 
($table_products.visible = 'Y' or $table_products.allowDirect = 'Y') 
GROUP BY 
$table_products.productID
ORDER BY 
$table_products.productID

 

The above works well, however, I had to add GROUP BY $table_products.productID to prevent multiple items. Although this works great, it's now causing an issue with the results. Before I added GROUP BY, each product would return with multiple values from this column $table_extrafields_values.extraFieldID=4

 

What I need to do is show one item but with multiple results from here. To help explain, the item results are shows and $table_extrafields_values.extraFieldID=4 is the shoe sizes.

 

So it was showing

PROD ID = 1

SHOE NAME = DM

SIZE = 7

 

PROD ID = 1

SHOE NAME = DM

SIZE = 8

 

PROD ID = 1

SHOE NAME = DM

SIZE = 9

 

What I need to do is

 

PROD ID = 1

SHOE NAME = DM

SIZE = 7,8,9

 

I know it can be done but I can't remember how, I think it's Concat or Group By

 

Can someone help?

 

Thanks

 

 

 

Link to comment
Share on other sites

Ah I think I've sussed it:

SELECT 
$table_products.productID,
$table_products.code,
$table_products.name,
$table_products.shortdescription,
$table_products.description,
$table_products.thumbnail,
$table_products.mainimage,
$table_products.extrafield1,
$table_products.extrafield2,
$table_products.extrafield3,
$table_products.price1,
$table_products.weight,
$table_products.scLevel,
group_concat($table_extrafields_values.content separator ', ') 
FROM $table_products LEFT JOIN $table_extrafields_values
ON $table_products.productID = $table_extrafields_values.productID
AND $table_extrafields_values.extraFieldID=4
WHERE (
   $table_products.productID 
   BETWEEN 2 And 999999) 
AND 
($table_products.visible = 'Y' or $table_products.allowDirect = 'Y') 
GROUP BY 
$table_products.productID
ORDER BY 
$table_products.productID

I think that does the job I need!

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.