简体   繁体   English

MySQL使用group_concat将多个关系表选择为一行

[英]MySQL selecting multiple relational tables into one row with group_concat

I have 5 tables: productCategory, products, productOptions, options, optionsGroup. 我有5个表:productCategory,products,productOptions,options,optionsGroup。 What I'm trying to do is somehow concat and group them into one query to return one row with all information about one product. 我正在尝试做的是以某种方式将其合并并分组为一个查询,以返回包含有关一种产品的所有信息的一行。 For example, a certain type of food will have different sizes and different flavors; 例如,某种类型的食物将具有不同的大小和风味。 bottles have different sizes and smells. 瓶子有不同的大小和气味。 Here are the tables: 表格如下:

productCategory 产品分类

categoryId    categoryName
1             food
2             freshener

product 产品

productId    productCategoryRef    productName
1            1                     ....
2            1
3            2
4            2
FK references categoryId

productOptions productOptions

categoryRef  optionsRef
1            1
1            3
2            2
2            4
FK categoryRef references productCategory (categoryId)
FK optionsRef references options (optionsId)

options 选项

optionsId    optionsName
1            kg
2            ml
3            Flavor
4            Fragrance

optionsGroup optionsGroup

groupId    groupOptionRef  groupName
1          1               3
2          1               7
3          1               14
4          2               50
5          2               250
6          3               Chicken
7          3               Beef
8          4               Alpine
9          4               Berries
FK groupOptionRef references options (optionId)

So a food bag can be 3kg, 7kg or 14kg and can have two different flavors. 因此,一个食品袋可以是3kg,7kg或14kg,并可以具有两种不同的口味。 How can I get all this information in one row in a readable format? 我如何以可读格式将所有这些信息排成一行? Here's what I tried: 这是我尝试过的:

SELECT 
  pc.*, p.*, 
  GROUP_CONCAT(DISTINCT o.optionsName) AS optionName, 
  GROUP_CONCAT(og.groupName) AS options 
FROM productCategories pc 
  JOIN products p ON p.productCategoryId=pc.categoryId 
  JOIN productOptions po ON po.categoryRef=pc.categoryId 
  JOIN options o ON o.optionsId=po.optionsRef 
  JOIN optionsGroup og ON og.groupOptionRef=o.optionsId 
WHERE p.productName=:itemName 
  GROUP BY p.productId

This gives me one row with 这给了我一排

....
optionName: 'kg,Flavor',
options: '3,7,14,Chicken,Beef'
....

which is a mess. 一团糟。 Is it possible to somehow concat each option together and then post-process it easier without having to guess which part goes to kg/Flavor? 是否有可能以某种方式将每个选项合并在一起,然后更容易地进行后处理,而不必猜测哪个部分达到kg / Flavor?

I know it would be easy here but later there will be a review section which will reference this productId which I would like to join as well to this query. 我知道这很容易,但是稍后会有一个回顾部分,它将引用我也想加入此查询的productId。 Any advice is appreciated, I'm open to a better DB structure if one is needed. 任何建议都值得赞赏,如果需要的话,我愿意采用更好的数据库结构。

If you don't mind option names being repeated, you can group_concat a concat like this: 如果您不介意重复使用选项名称,则可以使用group_concat这样的concat:

GROUP_CONCAT(CONCAT(o.optionsName, ': ', og.groupName) ORDER BY o.optionsName, og.groupName) AS options 

If you want any more structured than that, you'd have to subquery (grouping by option name, and group_concating it's group names; then grouping without option name in the outer query); 如果您想要更多的结构化,则必须进行子查询(按选项名称分组,并用group_conc表示其组名称;然后在外部查询中不带选项名称进行分组); but at that point it is definitely worth considering if you should just be handling formatting and grouping the results client side. 但是在那一点上,绝对值得考虑的是,您是否应该只处理客户端的格式设置和结果分组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM