问题
Consider in Couchbase I have a person database, with 3 documents:
Person A
Child AA
Grand AAA
Grand AAB
Child AB
Grand ABA
Grand ABB
Person B
Person C
Child CA
Child CB
Grand CBA
Is there an easy way to turn this into following results using N1QL? I am trying to display each person with their grand children displayed into strings?
Person A, Grand: "AAA, AAB, ABA, ABB"
Person B, Grand: ""
Person C, Grand: "CBA"
回答1:
You can do the following:
SELECT p.name AS p, ENCODE_JSON(ARRAY_AGG(g.name)) AS g
FROM person AS p LEFT OUTER UNNEST p.child AS c LEFT OUTER UNNEST c.grand AS g
GROUP BY p;
来源:https://stackoverflow.com/questions/35742657/n1ql-concatenate-many-children-rows-into-single-string