计数无法在group_concat MySQL内部工作

时间:2018-08-02 19:13:50

标签: mysql

我正在尝试在group_concat中合并id和1的计数。以下是我的查询。

MYSQL:

SELECT
months.name AS NAME,
CONCAT(
    '[',
    GROUP_CONCAT(
        CONCAT(
            '[\"',
            COUNT(p_c_n_details.JPN_ID),
            '\",',
            1,
            ']'
        )
    ),
    ']'
) AS DATA
FROM
p_c_n_details
INNER JOIN in_e_s_s__p_c_ns RIGHT OUTER JOIN months ON months.id = 
MONTH(p_c_n_details.created_at) AND p_c_n_details.type IN('Process Change', 
'Design Change') AND p_c_n_details.JPN_ID = 
in_e_s_s__p_c_ns.juniperinternalpcnid AND p_c_n_details.created_at >= 
last_day(NOW()) + INTERVAL 1 DAY - INTERVAL 3 MONTH
WHERE
months.name IN(
    MONTHNAME(
        DATE_SUB(CURDATE(), INTERVAL 0 MONTH)),
        MONTHNAME(
            DATE_SUB(CURDATE(), INTERVAL 1 MONTH)),
            MONTHNAME(
                DATE_SUB(CURDATE(), INTERVAL 2 MONTH))
            )
        GROUP BY
            months.id

预期输出:

Name   |     DATA
-------------------------
July        [20,1]
August       [33,1]

Table months contains month names.

但是我遇到错误#1111 - Invalid use of group function.,我尝试了此链接 mysql group_concat with a count inside? ,但在查询中使用时遇到错误。

1 个答案:

答案 0 :(得分:1)

这是一个猜测。

尝试:

Inserted  5  at the head

Printing the nodes
5  -->  NULL 

Inserted  10  at the head

Printing the nodes
10  --> 5  -->  NULL 

Inserted  1  at the head

Printing the nodes
1  --> 10  --> 5  -->  NULL 

Inserted  20  at end

Printing the nodes
1  --> 10  --> 5  --> 20  -->  NULL 

代替您现在拥有的SELECT。

您的示例结果不显示需要SELECT months.name AS NAME, CONCAT('{',COUNT(p_c_n_details.JPN_ID),',1}' AS DATA