MySQL GROUP_CONCAT添加斜线

时间:2018-06-26 07:45:15

标签: mysql group-concat

我正在尝试以JSON格式返回民意调查的结果。我的第一个SQL是

<\/\1>

此SQL返回类似的内容:

pollQuestion_id标题值

163问题1 {“文本”:“否”,“总计”:599}

163问题1 {“ text”:“ Si”,“ total”:258}

在第二个SQL中,我使用SELECT fa.pollQuestion_id, q.title, JSON_OBJECT( 'text', qo.title, 'total', COUNT(fao.pollQuestionOption_id) ) values FROM pollFillAnswers fa INNER JOIN pollFills f ON fa.pollFill_id = f.pollFill_id INNER JOIN pollQuestions q ON fa.pollQuestion_id = q.pollQuestion_id INNER JOIN pollFillAnswerOptions fao ON fao.pollFillAnswer_id = fa.pollFillAnswer_id WHERE f.poll_id = 17 AND fa.type IN (3,4,5) GROUP BY fao.pollQuestionOption_id; GROUP BY pollQuestion_id将结果汇总到该SQL中

GROUP_CONCAT(values)

结果如下

SELECT JSON_OBJECT(
        'title', result.title, 
        'values', GROUP_CONCAT(values)
    ) answers 
    FROM (
        SELECT
            q.title,
            JSON_OBJECT(
                'text', qo.title, 
                'total', COUNT(fao.pollQuestionOption_id)
            ) values
        FROM pollFillAnswers fa 
        INNER JOIN pollFills f ON fa.pollFill_id = f.pollFill_id
        INNER JOIN pollQuestions q ON fa.pollQuestion_id = q.pollQuestion_id
        INNER JOIN pollFillAnswerOptions fao ON fao.pollFillAnswer_id = fa.pollFillAnswer_id 
        WHERE f.poll_id = 17 AND fa.type IN (3,4,5) 
        GROUP BY fao.pollQuestionOption_id
    ) AS result 
    GROUP BY result.pollQuestion_id;

163 {"title": "Question 1", "values": "{\"text\": \"No\", \"total\": 599},{\"text\": \"Si\", \"total\": 258}"} 在JSON中的所有字符串中添加斜杠,为什么?我尝试用group_contact()REPLACE()删除斜杠,但无法消除这种现象。

1 个答案:

答案 0 :(得分:0)

请尝试以下代码一次。并且如果不需要,请用''替换。

GROUP_CONCAT(REPLACE(values, ',', '\\,'))