2016-05-11 7 views
1

クエリを登録しよう。投稿テーブルには、という質問の回答の2種類の投稿が含まれています。コメントは質問と回答で作成されます。私の目的は、1つの質問にそれに関するコメント、それに対する答え、その答えに対するコメントを得ることです。また私は、私が取った各質問、回答、コメントの著者としてユーザーテーブルからユーザー名が必要です。私はPostgres 9.5を使用しており、json_agg()の機能を利用しています。繰り返しエントリは、私は3つのテーブル<strong>ポスト</strong>、<strong>コメント</strong>、および<strong>ユーザー</strong>を持って

私が必要とする出力例は、最初の例と似ているはずですが、繰り返し入力されます。

私がここで紛失していることは何ですか?グループ別の訂正は可能性があります。または、コメントを使って回答を集めるサブクエリは、それを行う方法ではありません。私がコメントテーブルの投稿からの左の結合をコメントアウトするとき、私は質問に関するコメントなしで望ましい結果を得る。また、サブクエリを含む左結合を取り消すと、私は期待どおりの非反復の結果を得て、再びそれは私が望む完全なデータセットではありません。これらは私の問題を解決するためにこれまでに収集したものです。私は必要なもの

[ 
    { 
    "post_id": "10", 
    "created_at": "2016-05-10T00:16:54.469Z", 
    "post_type": "question", 
    "post_title": "qwerty", 
    "post_text": "asdasd asda sdasd", 
    "post_author_id": 1, 
    "author": "isikfsc", 
    "parent_post_id": null, 
    "is_accepted": null, 
    "acceptor_id": null, 
    "answers": [ 
     { 
     "post_id": 17, 
     "created_at": "2016-05-10T04:58:56.350229", 
     "post_type": "answer", 
     "post_title": null, 
     "post_text": "222asda dasdad asdada", 
     "post_author_id": 1, 
     "author": "isikfsc", 
     "parent_post_id": 10, 
     "is_accepted": null, 
     "acceptor_id": null, 
     "comments": [ 
      { 
      "id": 5, 
      "created_at": "2016-05-10T10:56:30.220128", 
      "text": "qweqwe", 
      "author_id": 1, 
      "author": "isikfsc", 
      "parent_post_id": 17 
      }, 
      { 
      "id": 8, 
      "created_at": "2016-05-10T11:00:00.182991", 
      "text": "sasasd", 
      "author_id": 1, 
      "author": "isikfsc", 
      "parent_post_id": 17 
      } 
     ] 
     }, 
     { 
     "post_id": 14, 
     "created_at": "2016-05-10T04:19:19.005556", 
     "post_type": "answer", 
     "post_title": null, 
     "post_text": "asdasdasdasd", 
     "post_author_id": 1, 
     "author": "isikfsc", 
     "parent_post_id": 10, 
     "is_accepted": null, 
     "acceptor_id": null, 
     "comments": [ 
      { 
      "id": 2, 
      "created_at": "2016-05-10T05:25:34.671008", 
      "text": "qeqweqwe", 
      "author_id": 1, 
      "author": "isikfsc", 
      "parent_post_id": 14 
      } 
     ] 
     } 
    ], 
    "comments": [ 
     { 
      "id": 1, 
      "created_at": "2016-05-10T10:56:30.220128", 
      "text": "qweqwe", 
      "author_id": 1, 
      "author": "isikfsc", 
      "parent_post_id": 10 
     }, 
     { 
      "id": 4, 
      "created_at": "2016-05-10T11:00:00.182991", 
      "text": "sasasd", 
      "author_id": 1, 
      "author": "isikfsc", 
      "parent_post_id": 10 
     } 
    ] 
    } 
] 

私のクエリは次のとおりです。

[ 
    { 
    "post_id": "10", 
    "created_at": "2016-05-10T00:16:54.469Z", 
    "post_type": "question", 
    "post_title": "qwerty", 
    "post_text": "asdasd asda sdasd", 
    "post_author_id": 1, 
    "parent_post_id": null, 
    "is_accepted": null, 
    "acceptor_id": null, 
    "answers": [ 
     { 
     "post_id": 17, 
     "created_at": "2016-05-10T04:58:56.350229", 
     "post_type": "answer", 
     "post_title": null, 
     "post_text": "222asda dasdad asdada", 
     "post_author_id": 1, 
     "parent_post_id": 10, 
     "is_accepted": null, 
     "acceptor_id": null, 
     "comments": [ 
      { 
      "id": 5, 
      "created_at": "2016-05-10T10:56:30.220128", 
      "text": "qweqwe", 
      "author_id": 1, 
      "parent_post_id": 17 
      }, 
      { 
      "id": 8, 
      "created_at": "2016-05-10T11:00:00.182991", 
      "text": "sasasd", 
      "author_id": 1, 
      "parent_post_id": 17 
      } 
     ] 
     }, 
     { 
     "post_id": 17, 
     "created_at": "2016-05-10T04:58:56.350229", 
     "post_type": "answer", 
     "post_title": null, 
     "post_text": "222asda dasdad asdada", 
     "post_author_id": 1, 
     "parent_post_id": 10, 
     "is_accepted": null, 
     "acceptor_id": null, 
     "comments": [ 
      { 
      "id": 5, 
      "created_at": "2016-05-10T10:56:30.220128", 
      "text": "qweqwe", 
      "author_id": 1, 
      "parent_post_id": 17 
      }, 
      { 
      "id": 8, 
      "created_at": "2016-05-10T11:00:00.182991", 
      "text": "sasasd", 
      "author_id": 1, 
      "parent_post_id": 17 
      } 
     ] 
     }, 
     { 
     "post_id": 17, 
     "created_at": "2016-05-10T04:58:56.350229", 
     "post_type": "answer", 
     "post_title": null, 
     "post_text": "222asda dasdad asdada", 
     "post_author_id": 1, 
     "parent_post_id": 10, 
     "is_accepted": null, 
     "acceptor_id": null, 
     "comments": [ 
      { 
      "id": 5, 
      "created_at": "2016-05-10T10:56:30.220128", 
      "text": "qweqwe", 
      "author_id": 1, 
      "parent_post_id": 17 
      }, 
      { 
      "id": 8, 
      "created_at": "2016-05-10T11:00:00.182991", 
      "text": "sasasd", 
      "author_id": 1, 
      "parent_post_id": 17 
      } 
     ] 
     }, 
     { 
     "post_id": 17, 
     "created_at": "2016-05-10T04:58:56.350229", 
     "post_type": "answer", 
     "post_title": null, 
     "post_text": "222asda dasdad asdada", 
     "post_author_id": 1, 
     "parent_post_id": 10, 
     "is_accepted": null, 
     "acceptor_id": null, 
     "comments": [ 
      { 
      "id": 5, 
      "created_at": "2016-05-10T10:56:30.220128", 
      "text": "qweqwe", 
      "author_id": 1, 
      "parent_post_id": 17 
      }, 
      { 
      "id": 8, 
      "created_at": "2016-05-10T11:00:00.182991", 
      "text": "sasasd", 
      "author_id": 1, 
      "parent_post_id": 17 
      } 
     ] 
     }, 
     { 
     "post_id": 14, 
     "created_at": "2016-05-10T04:19:19.005556", 
     "post_type": "answer", 
     "post_title": null, 
     "post_text": "asdasdasdasd", 
     "post_author_id": 1, 
     "parent_post_id": 10, 
     "is_accepted": null, 
     "acceptor_id": null, 
     "comments": [ 
      { 
      "id": 2, 
      "created_at": "2016-05-10T05:25:34.671008", 
      "text": "qeqweqwe", 
      "author_id": 1, 
      "parent_post_id": 14 
      } 
     ] 
     }, 
     { 
     "post_id": 14, 
     "created_at": "2016-05-10T04:19:19.005556", 
     "post_type": "answer", 
     "post_title": null, 
     "post_text": "asdasdasdasd", 
     "post_author_id": 1, 
     "parent_post_id": 10, 
     "is_accepted": null, 
     "acceptor_id": null, 
     "comments": [ 
      { 
      "id": 2, 
      "created_at": "2016-05-10T05:25:34.671008", 
      "text": "qeqweqwe", 
      "author_id": 1, 
      "parent_post_id": 14 
      } 
     ] 
     }, 
     { 
     "post_id": 14, 
     "created_at": "2016-05-10T04:19:19.005556", 
     "post_type": "answer", 
     "post_title": null, 
     "post_text": "asdasdasdasd", 
     "post_author_id": 1, 
     "parent_post_id": 10, 
     "is_accepted": null, 
     "acceptor_id": null, 
     "comments": [ 
      { 
      "id": 2, 
      "created_at": "2016-05-10T05:25:34.671008", 
      "text": "qeqweqwe", 
      "author_id": 1, 
      "parent_post_id": 14 
      } 
     ] 
     }, 
     { 
     "post_id": 14, 
     "created_at": "2016-05-10T04:19:19.005556", 
     "post_type": "answer", 
     "post_title": null, 
     "post_text": "asdasdasdasd", 
     "post_author_id": 1, 
     "parent_post_id": 10, 
     "is_accepted": null, 
     "acceptor_id": null, 
     "comments": [ 
      { 
      "id": 2, 
      "created_at": "2016-05-10T05:25:34.671008", 
      "text": "qeqweqwe", 
      "author_id": 1, 
      "parent_post_id": 14 
      } 
     ] 
     } 
    ], 
    "comments": [ 
     { 
     "id": 1, 
     "created_at": "2016-05-10T05:25:28.200327", 
     "text": "asadasdad", 
     "author_id": 1, 
     "parent_post_id": 10 
     }, 
     { 
     "id": 4, 
     "created_at": "2016-05-10T10:25:23.381177", 
     "text": "werwer", 
     "author_id": 1, 
     "parent_post_id": 10 
     }, 
     { 
     "id": 1, 
     "created_at": "2016-05-10T05:25:28.200327", 
     "text": "asadasdad", 
     "author_id": 1, 
     "parent_post_id": 10 
     }, 
     { 
     "id": 4, 
     "created_at": "2016-05-10T10:25:23.381177", 
     "text": "werwer", 
     "author_id": 1, 
     "parent_post_id": 10 
     }, 
     { 
     "id": 1, 
     "created_at": "2016-05-10T05:25:28.200327", 
     "text": "asadasdad", 
     "author_id": 1, 
     "parent_post_id": 10 
     }, 
     { 
     "id": 4, 
     "created_at": "2016-05-10T10:25:23.381177", 
     "text": "werwer", 
     "author_id": 1, 
     "parent_post_id": 10 
     }, 
     { 
     "id": 1, 
     "created_at": "2016-05-10T05:25:28.200327", 
     "text": "asadasdad", 
     "author_id": 1, 
     "parent_post_id": 10 
     }, 
     { 
     "id": 4, 
     "created_at": "2016-05-10T10:25:23.381177", 
     "text": "werwer", 
     "author_id": 1, 
     "parent_post_id": 10 
     } 
    ] 
    } 
] 

答えて

1

グループは、一度すべての当事者のは、集計ので、参加してきたが起こる:私は何を得る

SELECT 
    q.*, 
    json_agg(ac.*) AS answers, 
    json_agg(c.*) AS comments --comments on posts of post_id questions 
FROM posts q 

LEFT JOIN 
    (
     SELECT 
      a.*, 
      json_agg(c.*) AS comments -- comments on posts of post_id answers 
     FROM posts a 
     LEFT JOIN comments c 
     ON a.post_id = c.parent_post_id 

     GROUP BY a.post_id 
    ) ac 
ON q.post_id = ac.parent_post_id 

LEFT JOIN comments c 
ON q.post_id = c.parent_post_id 

WHERE q.post_id = 10 
GROUP BY q.post_id 

結果の基数に依存します。投稿と回答とコメントを結合すると、それらの間で完全な結合が行われ、すべての値が複製されます。また

SELECT 
    q.*, 
    (SELECT json_agg(ac.*) 
    FROM (
     SELECT a.*, json_agg(c.*) AS comments 
     FROM posts a 
     LEFT JOIN comments c ON (a.post_id = c.parent_post_id) 
     WHERE a.parent_post_id = q.post_id 
     GROUP BY a.post_id 
     ) ac 
    ) AS answers, 
    json_agg(c.*) AS comments --comments on posts of post_id questions 
FROM posts q 
LEFT JOIN comments c ON (q.post_id = c.parent_post_id) 
WHERE q.post_id = 10 
GROUP BY q.post_id; 

::彼らは分離して個別に実行する必要がある、あなたがそれを行うことができます一つの方法は以下の通りです

SELECT q.*, qa.answers, qc.comments 
FROM posts q 
LEFT JOIN (
    SELECT ac.parent_post_id, json_agg(ac.*) AS answers 
    FROM (
    SELECT ac.*, json_agg(c.*) AS comments 
    FROM posts ac 
    LEFT JOIN comments c ON (c.parent_post_id = ac.post_id) 
    GROUP BY ac.post_id 
    ) ac 
    GROUP BY ac.parent_post_id 
) qa ON (qa.parent_post_id = q.post_id) 
LEFT JOIN (
    SELECT c.parent_post_id, json_agg(c.*) AS comments 
    FROM comments c 
    GROUP BY c.parent_post_id 
) qc ON (qc.parent_post_id = q.post_id) 
WHERE q.post_id = 10; 
+0

私はターミナルでこれらのクエリからの2つのエラーメッセージを取得します。最初のものは前者のものです: '[エラー:構文エラー(またはその近くの"。 "]')と、後者の2つ目は '[エラー:FROM"]または '近くの構文エラーです。なぜ私は見つけることができませんでしたか?私は把握するためにチェックし続けます。 –

+0

'LEFT JOINコメントc(q.post_id = c.parent_post_id)'あなたはLEFT JOINコメントをcにしたのですか?q.post_id = c.parent_post_id' –

+0

@Işıkはい、両方のバグを修正しました –

関連する問題

 関連する問題