mysql中的COUNT和GROUP_CONCAT

时间:2018-08-06 12:50:25

标签: mysql group-concat

我有三个表pcn_typein_e_s_s__p_c_nsp_c_n_details。我正在尝试使用concat组将三个不同的值合并为一个。

我的查询:

SELECT 'browser' AS NAME, CONCAT( '[', CONCAT('{"', pcn_type.name, '",', 
COUNT(JPN_ID), '}'), ']' ) AS DATA FROM p_c_n_details INNER JOIN 
in_e_s_s__p_c_ns RIGHT OUTER JOIN pcn_type ON pcn_type.name = 
p_c_n_details.type AND in_e_s_s__p_c_ns.pcnid= 
p_c_n_details.JPN_ID GROUP BY pcn_type.name

结果获得了

NAME    |    DATA
-------------------------------------
browser      [{"Design Change",4}]
browser      [{"EOL",10}]
browser      [{"Process Change",21}]

预期结果:

NAME    |    DATA
--------------------------------------------------------------------
browser      [{"Design Change",4},{"EOL",10},{"Process Change",21}]

如何重组上述查询以获得预期结果。

1 个答案:

答案 0 :(得分:1)

使用GROUP_CONCAT函数

 select name,GROUP_CONCAT(DATA  SEPARATOR ' ') 
from 
(          
    SELECT 'browser' AS NAME, CONCAT( '[', CONCAT('{"', pcn_type.name, '",', 
    COUNT(JPN_ID), '}'), ']' ) AS DATA FROM p_c_n_details INNER JOIN 
    in_e_s_s__p_c_ns RIGHT OUTER JOIN pcn_type ON pcn_type.name = 
    p_c_n_details.type AND in_e_s_s__p_c_ns.pcnid= 
    p_c_n_details.JPN_ID GROUP BY pcn_type.name
) as T group by name