如何使用 Group_concat 获取每个产品的类别列表?

How to use Group_concat to get a list of categories for each product?

我有一个基于 OpenCart 3 的多语言网站,我在下面包含了必要的 tables 和 SQLFiddle。

我需要每个产品的逗号分隔列表 category name,如下所示:
product_id, language_id, product_name
其中 product_name 是逗号分隔的类别列表。 产品名称示例:Nightgown, Wrap robes, 2li
实际上是类别:8, 188, 192

重要提示: 最后,我想用逗号分隔的列表填充 product_description table 的 name 列我上面解释的类别。

看来我应该使用GROUP_CONCAT,但是我SQL虽然花了半天多时间,但它远远超出了我的知识范围。

这是 SQL小提琴:http://sqlfiddle.com/#!9/0337c3

架构

CREATE TABLE `category` (
    `category_id` int(11) NOT NULL,
    `parent_id` int(11) NOT NULL DEFAULT '0'
) 

CREATE TABLE `category_description` (
    `cd_id` int(11) NOT NULL,
    `category_id` int(11) NOT NULL,
    `language_id` int(11) NOT NULL,
    `name` varchar(255) NOT NULL
) 

CREATE TABLE `language` (
    `language_id` int(11) NOT NULL,
    `name` varchar(32) NOT NULL,
    `code` varchar(5) NOT NULL,
    `locale` varchar(255) NOT NULL,
    `image` varchar(64) NOT NULL,
    `directory` varchar(32) NOT NULL,
    `sort_order` int(3) NOT NULL DEFAULT '0',
    `status` tinyint(1) NOT NULL
) 

CREATE TABLE `product` (
    `product_id` int(11) NOT NULL,
    `model` varchar(64) NOT NULL,
    `price` decimal(15,4) NOT NULL DEFAULT '0.0000'
) 

CREATE TABLE `product_description` (
    `product_id` int(11) NOT NULL,
    `language_id` int(11) NOT NULL
) 

CREATE TABLE `product_to_category` (
    `product_id` int(11) NOT NULL,
    `category_id` int(11) NOT NULL
) 


ALTER TABLE `category`
    ADD PRIMARY KEY (`category_id`,`parent_id`),
    ADD KEY `parent_id` (`parent_id`);

ALTER TABLE `category_description`
    ADD PRIMARY KEY (`cd_id`,`category_id`,`language_id`),
    ADD KEY `name` (`name`);

ALTER TABLE `language`
    ADD PRIMARY KEY (`language_id`),
    ADD KEY `name` (`name`);

ALTER TABLE `product`
    ADD PRIMARY KEY (`product_id`);

ALTER TABLE `product_description`
    ADD PRIMARY KEY (`product_id`,`language_id`),
    ADD KEY `name` (`name`);

ALTER TABLE `product_to_category`
    ADD PRIMARY KEY (`product_id`,`category_id`),
    ADD KEY `category_id` (`category_id`);


ALTER TABLE `category`
    MODIFY `category_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=202;
ALTER TABLE `category_description`
    MODIFY `cd_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=961;
ALTER TABLE `language`
    MODIFY `language_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=6;
ALTER TABLE `product`
    MODIFY `product_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=85;

如果你能告诉我问题出在哪里,我将不胜感激?

您可以使用以下 select 语句:

select
    pd.product_id,
    pd.language_id,
    group_concat(cd.name order by cd.language_id separator ', ') as product_name
from product_description pd
left join product_to_category pc using (product_id)
left join category_description cd using (category_id, language_id)
group by pd.product_id, pd.language_id

我认为将冗余数据存储在新列中不是一个好主意。但是你可以这样做:

update product_description pd
join (
  select
      pd.product_id,
      pd.language_id,
      group_concat(cd.name order by cd.language_id separator ', ') as product_name
  from product_description pd
  left join product_to_category pc using (product_id)
  left join category_description cd using (category_id, language_id)
  group by pd.product_id, pd.language_id
) sub using (product_id, language_id)
set pd.name = sub.product_name;

以下查询应该有效:

select pd.product_id, pd.language_id,group_concat(distinct cd.name separator ',') as Product_name
from product_description pd
inner join product_to_category pc
on pc.product_id = pd.product_id
inner join category_description cd
on pc.category_id = cd.category_id and pd.language_id = cd.language_id
group by pd.product_id, pd.language_id;

Click here for Demo

要更新table:

Update product_description p
set p.name = (select group_concat(distinct cd.name separator ',') as Product_name
from (select *from product_description) pd
inner join product_to_category pc
on pc.product_id = pd.product_id
inner join category_description cd
on pc.category_id = cd.category_id and pd.language_id = cd.language_id
group by pd.product_id, pd.language_id
having p.product_id = pd.product_id and p.language_id = pd.language_id);

SqlFiddle

希望对您有所帮助!