MySQL加入并包含

时间:2014-12-04 22:30:03

标签: mysql join

我有五张桌子,这是一个很大的混乱,我试图加入。

Tbl - USERS
id - name
---------
1
2
3
4
5

Tbl - SESSION1
id | user_id | date
--------------------
1    1         1/1/11
2    2         1/2/11
3    3         1/2/11
4    5         1/3/11

Tbl - SESSION2
id | user_id | date
--------------------
1    1         1/1/12
2    2         1/1/12
3    4         1/2/12
5    5         1/2/12

Table - QA
id | user_id | session_id | question                      | answer
--------------------------------------------------------------------
1    1         1            Did you go on vacation?         Y
2    1         1            Did you like it there?          N
3    1         2            Did you have travel insurance?  N
4    3         1            Did you go on vacation?         Y
5    3         1            Did you like it there?          N
6    3         1            Did you have travel insurance?  N

Table - QA2
id | user_id | session_id | question         | answer
--------------------------------------------------------
1    1         1            Country Visited    Cuba
2    1         2            Country Visited    Mexico

我想运行一个查询,以便在连接中获得以下数据,这是最有效的。我需要检查用户是否回答了问题,“你去过度假吗。”

user_id | session 1 | session 2 | question             | answer | country
---------------------------------------------------------------------------
1         1/1/11    null          been on vacation       yes      cuba
1         1/1/11    null          had travel insurance   no       cuba
1         null      1/1/12        been on vacation       yes      mexico
1         null      1/1/12        had travel insurance   no       mexico

更新/免责声明:我没有设计此架构,我无法进行更改。

0 个答案:

没有答案