2010-01-26 2 views
0

안녕하세요 DBA와 전반적인 친구들. 나는 너에게 질문이있다.GROUP_CONCAT 질문을 사용하는 JSON VIEW

MySQL의 VIEW를 사용하여 우리의 데이터를 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

같은 VIEW를 만드는

봅니다 데이터베이스에 직접 쿼리를 실행 노력했다. group_concat에 대한 출력에서 ​​경고가 표시됩니까? group_concat은 1024 자의 자연 한도에 쉽게 도달 할 수 있습니다. (증가시킬 수 있습니다) – MindStalker

+0

이미'global_group_concat_max_len = 8192;를 설정했습니다 (충분합니다). 그러나 이런 일이 생겨도 텍스트가 잘려서 (단지'NULL' 만 얻고 있습니다). 좋은 추측, 그 전에 문제가되었습니다 (내 JSON 엉망이) –

+0

가능한 복제 [그룹 - concat mysql과 json 형식을 만드는 방법?] (http://stackoverflow.com/questions/12511933/how-create- group-concat-mysql) – e4c5

답변

0

일부 테스트를 거쳤으며 오류를 찾을 수 없습니다. THEN

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 
+0

예, VIEW를 작성한 다음 그 데이터를 가져 오는 것이 좋겠지 만 모든 인라인으로 처리하고 싶습니다. (이미 너무 많은 뷰가 있습니다.) –