1

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

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

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

看来我应该使用GROUP_CONCAT,但它远远超出了我的 SQL 知识,即使我花了半天以上的时间。

这是 SQLFiddle: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;

如果你能告诉我有什么问题,我将不胜感激?

4

2 回答 2

2

您可以使用以下选择语句:

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;
于 2017-10-24T20:14:15.087 回答
1

以下查询应该有效:

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;

点击这里进行演示

要更新表格:

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

希望能帮助到你!

于 2017-10-24T20:17:05.730 回答