Сводная таблица между горизонтальными и вертикальными таблицами mysql - PullRequest
1 голос
/ 07 ноября 2019

У меня есть вопросы и ответы, для которых ниже приведена структура моей таблицы

select * from tbl_test_question;

q_id |   que          | opt1  |   opt2 |   opt3 |   opt4 |   ans
------------------------------------------------------------------
 1   |  what is this? |  cat  |   dog  |   owl  |   cow  |   opt2
 2   |  what is that? |  man  |   pig  |   bat  |   rat  |   opt4
 3   |  who is this?  |  ice  |   flea |   dirt |   lion |   opt1
 4   |  who is that?  |  goat |   meat |   kid  |   gin  |   opt3

select * from tbl_user_answer

 uid |   qid |   submit_ans |   correct_ans
-------------------------------------------------
  1  |    1  |     opt2     |       opt2
  1  |    2  |     opt3     |       opt4
  1  |    3  |     opt4     |       opt1 
  1  |    4  |     opt1     |       opt3

Присоединение к обоим Iхотел бы, чтобы мой результат выглядел следующим образом.

qid  |  submit_ans |  submittext |  correct_ans |  correcttext
-------------------------------------------------------------
 1   |     opt2    |    dog      |     opt2     |     dog             
 2   |     opt3    |    bat      |     opt4     |     rat                  
 3   |     opt4    |   lion      |     opt1     |     ice                  
 4   |     opt1    |   goat      |     opt3     |     kid                  

Я не смог придумать, как это выглядит так. Я никогда не делал разворот раньше. Есть ли запрос, по которому я мог бы получить результат?

Ответы [ 2 ]

1 голос
/ 07 ноября 2019

Слишком длинный и умный для комментария ...

Хотя это и не претендует на окончательное решение, вот пример нормализованной схемы:

tbl_test_question:

+------+---------------+
| q_id | question      |
+------+---------------+
|    1 | what is this? | 
|    2 | what is that? | 
|    3 | who is this?  | 
|    4 | who is that?  | 
+------+---------------+

tbl_user_answer:

+------+--------+-------+---------+
| q_id | option | value | correct |
+------+--------+-------+---------+
|    1 |      1 | cat   |       0 | 
|    2 |      1 | man   |       1 |
|    3 |      1 | ice   |       0 | 
|    4 |      1 | goat  |       0 | 
|    1 |      2 | dog   |       0 |  
|    2 |      2 | pig   |       0 | 
|    3 |      2 | flea  |       0 |  
|    4 |      2 | meat  |       1 |
|    1 |      3 | owl   |       0 |
|    2 |      3 | bat   |       0 | 
|    3 |      3 | dirt  |       0 |  
|    4 |      3 | kid   |       0 | 
|    1 |      4 | cow   |       0 |  
|    2 |      4 | rat   |       0 | 
|    3 |      4 | lion  |       1 |
|    4 |      4 | gin   |       0 |  
+------+--------+-------+---------+
0 голосов
/ 07 ноября 2019

Вам необходимо присоединиться к таблицам и использовать выражения CASE, например:

select
  q.q_id qid, u.submit_ans submit_ans,
  case u.submit_ans
    when 'opt1' then q.opt1
    when 'opt2' then q.opt2
    when 'opt3' then q.opt3
    when 'opt4' then q.opt4
  end submit_text,
  u.correct_ans,
    case u.correct_ans
    when 'opt1' then q.opt1
    when 'opt2' then q.opt2
    when 'opt3' then q.opt3
    when 'opt4' then q.opt4
  end correcttext
from tbl_test_question q inner join tbl_user_answer u
on u.qid = q.q_id

См. demo . Результаты:

> qid | submit_ans | submit_text | correct_ans | correcttext
> --: | :--------- | :---------- | :---------- | :----------
>   1 | opt2       | dog         | opt2        | dog        
>   2 | opt3       | bat         | opt4        | rat        
>   3 | opt4       | lion        | opt1        | ice        
>   4 | opt1       | goat        | opt3        | kid 
...