473,503 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Group_concat()

bilibytes
128 New Member
Hi,

I'm wondering how to achieve the following:

well i don't know if you guys like wine but I have to explain a very little on wine to explain my example: Wine is made from grape and a wine bottle may be made from different sorts of grape. So when you have a bottle it usually says: 10% of shyra, 50% of cabernet and 40% of merlot
meaning that the wine in the bottle was made from that percent of each grape type.

that was it for wine.

now SQL

need to store all the grapes that a wine has and their respective percentage.
so i have my bottles which are stored under a table called Wine
then i have all existing types of grapes stored in Grape
and i associate these two things by a third table called WineToGrape
its a one to many relationship.
here are the tables examples:

Expand|Select|Wrap|Line Numbers
  1. Wine
  2. wine_id | name
  3.  
  4. Grape
  5. grape_id | name
  6.  
  7. WineToGrape
  8. wtg_id | grape_id | wine_id | percentage
  9.  
Now the fun part: How do i retrieve a list of all the wines with all their respective grapes and percentage?

I see one option which implies PHP or any other language:
Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.     w.wine_id AS wineId, 
  3.     w.name AS wineName,
  4.     g.grape_id AS grapeId, 
  5.     g.name AS grapeName, 
  6.     wtg.percentage AS grapePercentageInWine 
  7. FROM 
  8.     WineToGrape AS wtg
  9.         INNER JOIN Wine AS w ON (wtg.wine_id = w.wine_id) 
  10.         INNER JOIN Grape AS g ON (wtg.grape_id = g.grape_id);
  11.  
Then i would end up with a big array that would have many duplicate wine_id's and i can't do a GROUP BY because it would get off the grapes and i want all the grapes
So from PHP i would have to loop through all the rows and if the wine_id in the row already exists then i would append the grape to the wine_id key like this:
Expand|Select|Wrap|Line Numbers
  1. $wines = array();
  2. while($row = mysql_fetch_array($resultset)){
  3.     //if the wine name was not already stored, store it
  4.     if (! isset($wines[$row['wineId']])) {
  5.         $wines[$row['wineId']]['name'] = $row['wineName'];
  6.     }
  7.     //any time there is a new record it the grape name and percetage has
  8.     //to be added to the wine element in the array
  9.         $wines[$row['wineId']]['grapes'][$row['grapeId']]['name'] = $row['grapeName'];
  10.         $wines[$row['wineId']]['grapes'][$row['grapeId']]['percentage'] = $row['grapePercentageInWine'];
  11.     }
  12. }
So an example result array would be:

Expand|Select|Wrap|Line Numbers
  1. array(3=>array('name'=>'wine3',
  2.                   'grapes'=> array(26=>array('name'=>'grape26',
  3.                                             'percentage'=>40),
  4.                                   34=>array('name'=>'grape26',
  5.                                             'percentage'=>60)
  6.                                  )
  7.                   ),
  8.      7=>array('name'=>'wine7',
  9.               'grapes'=> array(31=>array('name'=>'grape31',
  10.                                          'percentage'=>15),
  11.                               34=>array('name'=>'grape34',
  12.                                          'percentage'=>40),
  13.                               15=>array('name'=>'grape15',
  14.                                          'percentage'=>45)
  15.                              )
  16.  
  17.               ),
  18.      23=>array('name'=>'wine23',
  19.               'grapes'=> array(31=>array('name'=>'grape31',
  20.                                          'percentage'=>100),
  21.                              )
  22.               )
  23. );
Once i have my $wines array i would have to loop again through all the elements and nodes in the array to display them.

That is feasable but i have heard of another possibility:
GROUP_CONCAT() sql function

group concat would give me a concatenated string of all the grapes with this sql:

Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.     w.wine_id AS wineId, 
  3.     w.name AS wineName,
  4.     GROUP_CONCAT(g.grape_id) AS grapeId, 
  5.     g.name AS grapeName, 
  6.     wtg.percentage AS grapePercentageInWine 
  7. FROM 
  8.     WineToGrape AS wtg
  9.         INNER JOIN Wine AS w ON (wtg.wine_id = w.wine_id) 
  10.         INNER JOIN Grape AS g ON (wtg.grape_id = g.grape_id);
  11.  
But what i would not have my percentages, so that's where i need help:

I need to know how to use the GROUP_CONCAT() function to concatenate all the grapes and percentages like this: 'grape23 10%, grape11 90%' for each wine.

is that feasible ?

Thank you just for the fact of having read so far!

regards

bilibytes
Aug 18 '09 #1
1 5740
labmonkey111
44 New Member
In GROUP_CONCAT() you can put any expression you want, so you should be able to do something like this:

Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.     w.wine_id AS wineId, 
  3.     w.name AS wineName,
  4.     GROUP_CONCAT(CONCAT(g.name,' ',wtg.percentage)) AS grapeId
  5. FROM 
  6.     WineToGrape AS wtg
  7.         INNER JOIN Wine AS w ON (wtg.wine_id = w.wine_id) 
  8.         INNER JOIN Grape AS g ON (wtg.grape_id = g.grape_id);
  9.  
I think that will do what you need it to.
Aug 19 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
2669
by: peter stickney | last post by:
I am using the GROUP_CONCAT() as follows: SELECT (SELECT CONCAT(ProjectName) FROM Project WHERE FIND_IN_SET(ProjectID, RelatedProjectID))AS RelatedProject We have a project tracker system and...
2
1696
by: phillip.s.powell | last post by:
Here are my results using MySQL 4.1.12 and I honestly don't think this is right, but can't figure out what to do: mysql> select id, group_concat(DISTINCT school_enrollment_time_name SEPARATOR...
2
1911
by: phillip.s.powell | last post by:
SELECT s.id, s.student_first_name, s.student_last_name, IF(s.student_ethnicity_interest_other IS NOT NULL AND s.student_ethnicity_interest_other != '', CONCAT(s.student_ethnicity_interest_other,...
0
1214
by: phillip.s.powell | last post by:
$if1 = "IF(s.student_ethnicity_interest_other IS NOT NULL AND s.student_ethnicity_interest_other != '', " . " CONCAT(s.student_ethnicity_interest_other, ', ', GROUP_CONCAT(e.ethnicity_name...
0
3312
by: gmorris | last post by:
I am attempting to pull data from multiple tables into a resultset that I can use as an Excel spreadsheet for reporting. Here is my query so far: SELECT b.bug_id,p.realname as 'Assigned'...
1
2270
by: lukemack | last post by:
Hi, I have an array in the following format : Array ( =Array ( =Array (
11
7668
by: siva07 | last post by:
my $cStr="select distinct artist,company from albuminfo "; i am using this query to select unique artist and company. But the duplicates also get display. Is any idea. Thanks
2
3150
by: beargrease | last post by:
I'm kind of comfortable with basic joins, but a current project requires a complex query of many tables. The GROUP_CONCAT(DISTINCT ...) function has been very useful as returning my values as comma...
2
1776
rizwan6feb
by: rizwan6feb | last post by:
I have created a table (with only 2 fields) with the following query "CREATE TABLE `users` ( `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY , `name` VARCHAR( 20 ) NOT NULL ) ENGINE = MYISAM ;" ...
0
7086
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7280
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
382
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.