0

I'm using MySQL 5.1.66 and I came up with a select query to select all the men's shirts and their sizes and prices. I used GROUP CONCAT to merge the size column and price column from their parent tables into the same column. Here's the statement I made

SELECT shirts.shirt_name, shirts.men AS main_photo, 
       GROUP_CONCAT(shirt_sizes.size_name,shirt_prices.price) AS sizes
FROM shirts 
JOIN shirts_link ON shirts_link.shirt_id=shirts.id
JOIN shirt_sizes ON shirt_sizes.id=shirts_link.size_id
JOIN shirt_prices ON shirt_prices.id=shirts_link.price_id
WHERE shirts.men!='' AND shirts_link.adult='y'
GROUP BY shirts.id

By doing this, are the size columns and price columns still two separate entities within that one column in the select statement? Another thing I noticed is the order of the sizes is all mixed up. For example, this is a row from one of the columns

medium29.22,large29.22,1x-large29.22,2x-large30.44,small29.22,3x-large31.70

why isn't it going from small-3x like it's organized on the table? I imagine in the event of what I'm trying to do in terms of injecting it into we website through PHP if it were to auto load it would go in that unorganized manner as well. How can I fix this?

My ultimate goal here is to be able to create a selection that can auto populate each row into the javascript apps I created. I need the load the shirt name, main picture, then each size and price into the divs I have coded for them. Let me know if you need to see the actual tables being used. Thanks in advance :)

4

2 回答 2

1

首先 group_concat 函数的目的是将特定列的所有值保留在分组操作中。例如,

Table1
ID   field1    field2
1     ram      science
2     ramesh   maths
1     ram      maths

如果您的要求是获取特定人的主题,那么 GROUP_CONCAT 将产生如下结果,

ID  field1   field3
1   ram      science,maths
2   ramesh   maths

希望这能提供一些见解

于 2012-12-31T10:58:43.293 回答
0

尝试这个:

SELECT s.shirt_name, s.men AS main_photo, 
       GROUP_CONCAT(CONCAT(ss.size_name, '.', sp.price)) AS sizes
FROM shirts s 
INNER JOIN shirts_link sl ON sl.shirt_id = s.id
INNER JOIN shirt_sizes ss ON ss.id = sl.size_id
INNER JOIN shirt_prices sp ON sp.id = sl.price_id
WHERE s.men!='' AND sl.adult='y'
GROUP BY s.id
于 2012-12-31T12:55:20.370 回答