mysql 中的 COUNT 个和 GROUP_CONCAT 个
COUNT and GROUP_CONCAT in mysql
我有三个表 pcn_type
、in_e_s_s__p_c_ns
、p_c_n_details
。我正在尝试使用组连接将三个不同的值连接成一个。
我的查询:
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}]
如何重构上述查询以获得预期结果。
使用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
我有三个表 pcn_type
、in_e_s_s__p_c_ns
、p_c_n_details
。我正在尝试使用组连接将三个不同的值连接成一个。
我的查询:
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}]
如何重构上述查询以获得预期结果。
使用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