GROUP_CONCAT创建一个下拉列表

时间:2017-06-22 05:58:41

标签: php mysql

我的查询如下:

SELECT p.product_id, 
       Group_concat(DISTINCT Concat( optlan.option_label, ',', ovl.value) 
       SEPARATOR ';' 
       )                                                                     AS 
       options_01, 
       Concat(Group_concat(DISTINCT optlan.option_label), ';', 
       Group_concat(DISTINCT ovl.value))                                     AS 
       options_02, 
       Concat_ws(';', optlan.option_label, Group_concat(DISTINCT ovl.value)) AS 
       options_03 
FROM   products p 
       JOIN category c 
         ON p.category_id = c.category_id 
       LEFT JOIN options_categories opcat 
              ON opcat.category_id = c.category_id 
       LEFT JOIN options opt 
              ON opt.option_id = opcat.option_id 
       LEFT JOIN options_lang optlan 
              ON optlan.option_id = opt.option_id 
                 AND optlan.lang_id = 'NL' 
       LEFT JOIN option_values ov 
              ON ov.option_id = opt.option_id 
       LEFT JOIN option_values_lang ovl 
              ON ovl.value_id = ov.value_id 
                 AND ovl.lang_id = 'NL' 
WHERE  p.product_id = '25' 
GROUP  BY p.product_id;

options_01结果:

option a,40;option a,50;option b,60;option b,70

options_2结果:

option a,option b;40,50,60,70

和options_03:

option b;60,70

我想创建这个:

option a;40,50|option b;60,70

或者我是否需要在查询中使用PHP创建它?希望有人可以帮助我。

2 个答案:

答案 0 :(得分:0)

我不知道你的数据库/表的结构,但我认为你可以像这样修改你的查询(对你的案例进行所有修改):

        SELECT
            GROUP_CONCAT(options_try SEPARATOR '|') as your_field
        FROM (
            SELECT
                CONCAT(optlan.option_label, ';', GROUP_CONCAT(ovl.value SEPARATOR ",")) AS options_try
            FROM
                products p
            JOIN category c
                ON p.category_id = c.category_id
            LEFT JOIN options_categories opcat
                ON opcat.category_id = c.category_id
            LEFT JOIN options opt
                ON opt.option_id = opcat.option_id
            LEFT JOIN options_lang optlan
                ON optlan.option_id = opt.option_id AND optlan.lang_id = 'NL'
            LEFT JOIN option_values ov
                ON ov.option_id = opt.option_id
            LEFT JOIN option_values_lang ovl
                ON ovl.value_id = ov.value_id AND ovl.lang_id = 'NL'
            WHERE
                p.product_id = '25'
            GROUP
                BY optlan.option_label;
        ) x1

请在查询结尾处注意该组。如果不按“主”字段对结果进行分组,则不能使用group_concat。

答案 1 :(得分:0)

也许我确实让我的问题变得复杂,因为我可以更轻松地做到这一点。

假设我必须使用表格:

表1:options => option_id和option_label 表2:值=> value_id,option_id和value

CREATE TABLE `options` (
`option_id` int(5) UNSIGNED NOT NULL,
`option_label` varchar(100) NOT NULL DEFAULT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=COMPACT;

CREATE TABLE `values` (
`value_id` int(5) UNSIGNED NOT NULL,
`option_id` int(5) UNSIGNED NOT NULL ,
`value` varchar(100) NOT NULL DEFAULT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

表格选项:     插入optionsoption_idoption_label)值     (1,'选项a'),     (2,'选项b');

表值:     INSERT INTO option_valuesvalue_idoption_idvalue)VALUES     (1,1,' val 1a'),     (2,1,' val 2a'),     (3,1,' val 3a'),     (4,2,' val 1b'),     (5,2,' val 2b'),;

现在查询应如下所示:

SELECT
 GROUP_CONCAT ........... AS all_options
FROM options o
JOIN values v
ON o.option_id = v.option_id
GROUP BY o.option_id 

结果应该是这样的:

all_options 选项a; val 1a,val 2a,val 3a |选项b; val 1b,val 2b

希望这有道理.....