嗨DBA和整体聪明的家伙。我有一个问题要问你。
我们使用MySQL VIEWs将数据格式化为JSON(如BLOB),这很方便(虽然性能不是特别好,但我们已经知道了)。
但是,我现在似乎无法使特定查询正常工作(当每行包含一个具有多个JOIN值的已创建JSON对象时,每行包含NULL)。
以下是一般概念:
SELECT CONCAT(
"{",
"\"some_list\":[", GROUP_CONCAT( DISTINCT t1.id ), "],",
"\"other_list\":[", GROUP_CONCAT( DISTINCT t2.id ), "],",
"}"
) cool_json
FROM table_name tn
INNER JOIN ( some_table st ) ON st.some_id = tn.id
LEFT JOIN ( another_table at, another_one ao, used_multiple_times t1 )
ON st.id = at.some_id AND
at.different_id = ao.different_id AND
ao.different_id = t1.id
LEFT JOIN ( another_table2 at2, another_one2 ao2, used_multiple_times t2 )
ON st.id = at2.some_id AND
at2.different_id = ao2.different_id AND
ao2.different_id = t2.id
GROUP BY tn.id ORDER BY tn.name
有人知道这里的问题吗?我错过了一些我应该分组的东西吗?当我只做1 LEFT JOIN& GROUP_CONCAT,但现在有多个JOIN / GROUP_CONCAT,它搞乱了。
当我从“cool_json”字段移动GROUP_CONCAT时,它们按预期工作,但我希望我的数据格式化为JSON,因此我可以一步解码服务器端或客户端。
答案 0 :(得分:0)
我已经测试了其中一些并且找不到任何错误。
尝试创建像
这样的视图CREATE VIEW tn_view AS
SELECT tn.id, tn.name, t1.id, t2.id
FROM table_name tn
INNER JOIN ( some_table st ) ON st.some_id = tn.id
LEFT JOIN ( another_table at, another_one ao, used_multiple_times t1 )
ON st.id = at.some_id AND
at.different_id = ao.different_id AND
ao.different_id = t1.id
LEFT JOIN ( another_table2 at2, another_one2 ao2, used_multiple_times t2 )
ON st.id = at2.some_id AND
at2.different_id = ao2.different_id AND
ao2.different_id = t2.id
然后
SELECT CONCAT(
"{",
"\"some_list\":[", GROUP_CONCAT( DISTINCT t1.id ), "],",
"\"other_list\":[", GROUP_CONCAT( DISTINCT t2.id ), "],",
"}"
) cool_json
FROM tn_view
GROUP BY id ORDER BY name