Posgre SQL объединяет таблицу как массив - PullRequest
0 голосов
/ 24 марта 2020

У меня есть таблица, которая называется - вопросы:

 id |   type   | test_id |              text               
----+----------+---------+---------------------------------
  1 | single   |       1 | What you can do?
  2 | multiple |       2 | What he can do?
  3 | anything |       3 | What I can do?
  5 | multiple |       1 | Are you a man?
  8 | anything |       3 | 5
  6 | anything |       1 | What are you going to do today?

Также у меня есть таблица, которая называется - ответы:

 id | question_id |    answer     | is_right_answer 
----+-------------+---------------+-----------------
  1 |           1 | Everything    | t
  2 |           1 | Nothing       | f
  3 |           1 | That depends  | f
  4 |           2 | Everything    | f
  5 |           2 | Nothing       | t
  6 |           2 | That depends  | t
  7 |           3 | Everything    | t
  8 |           3 | Nothing       | t
  9 |           3 | That depends  | t

Я хочу выбрать по id из таблицы вопросов и получить что-то вроде этого:

 {
    id: 1,
    type: 'single',
    text: 'What you can do?',
    test_id: '1',
    answers: [{id: 1, question_id: 1, answer: 'Everything', is_right_answer: t}, {id: 12 question_id: 1, answer: 'Nothing', is_right_answer: f}]
  },

Итак, я хочу получить все связанные ответы на точный вопрос в виде массива.

Как я могу сделать это в postgreSQL?

Ответы [ 2 ]

0 голосов
/ 24 марта 2020

Предполагая, что вы хотите вывод JSON (B), вы можете сделать это с row_to_jsonb() и jsonb_agg():

select 
    q.*,
    (select jsonb_agg(row_to_json(a)) from answers a where a.question_id = q.id) answers
from questions q

Демонстрация на DB Fiddle :

id | type     | test_id | text                            | answers                                                                                                                                                                                                                                          
-: | :------- | ------: | :------------------------------ | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 1 | single   |       1 | What you can do?                | [{"id": "1", "answer": "Everything", "question_id": 1, "is_right_answer": true}, {"id": "2", "answer": "Nothing", "question_id": 1, "is_right_answer": false}, {"id": "3", "answer": "That depends", "question_id": 1, "is_right_answer": false}]
 2 | multiple |       2 | What he can do?                 | [{"id": "4", "answer": "Everything", "question_id": 2, "is_right_answer": false}, {"id": "5", "answer": "Nothing", "question_id": 2, "is_right_answer": true}, {"id": "6", "answer": "That depends", "question_id": 2, "is_right_answer": true}] 
 3 | anything |       3 | What I can do?                  | [{"id": "7", "answer": "Everything", "question_id": 3, "is_right_answer": true}, {"id": "8", "answer": "Nothing", "question_id": 3, "is_right_answer": true}, {"id": "9", "answer": "That depends", "question_id": 3, "is_right_answer": true}]  
 5 | multiple |       1 | Are you a man?                  | <em>null</em>                                                                                                                                                                                                                                             
 6 | anything |       1 | What are you going to do today? | <em>null</em>                                                                                                                                                                                                                                             
 8 | anything |       3 | 5                               | <em>null</em>                                                                                                                                                                                                                                             
0 голосов
/ 24 марта 2020

Вы можете использовать array_agg():

select q.*,
       (select array_agg(a.answer)
        from answers a
        where a.question_id = q.id
       ) as answers
from questions q;
...