我有三个表,我试图查询这三个表来获得我想要的结果(没有组合的行),但是,在结果中组合了行。
以下是表格:
monitoring table
id | code
12 | M-12
monitoring_question_log table
id | monitoring_id | monitoring_question_id | monitoring_question_choice_id | textual_answer
11 12 1 1 (NULL)
12 12 2 4 (NULL)
13 12 3 6 (NULL)
14 12 4 10 (NULL)
15 12 5 14 (NULL)
16 12 6 15 (NULL)
17 12 7 (NULL) N/A
monitoring_checklist_log table
id | monitoring_id | monitoring_checklist_id | answer
23 12 1 checked
24 12 2 unchecked
25 12 3 checked
26 12 4 checked
以下是我使用的查询:
SELECT
m.id,
m.code,
mql.monitoring_question_choice_id,
mql.textual_answer,
mcl.monitoring_checklist_id,
mcl.answer
FROM monitoring m
LEFT JOIN monitoring_question_log mql ON mql.monitoring_id = m.id
LEFT JOIN monitoring_checklist_log mcl ON mcl.monitoring_id = m.id
WHERE m.id = 12
上述查询的结果是:
id code monitoring_question_choice_id textual_answer monitoring_checklist_id answer
12 M-12 1 (NULL) 1 checked
12 M-12 1 (NULL) 2 unchecked
12 M-12 1 (NULL) 3 checked
12 M-12 1 (NULL) 4 checked
12 M-12 4 (NULL) 1 checked
12 M-12 4 (NULL) 2 unchecked
12 M-12 4 (NULL) 3 checked
12 M-12 4 (NULL) 4 checked
12 M-12 6 (NULL) 1 checked
12 M-12 6 (NULL) 2 unchecked
12 M-12 6 (NULL) 3 checked
12 M-12 6 (NULL) 4 checked
12 M-12 10 (NULL) 1 checked
12 M-12 10 (NULL) 2 unchecked
12 M-12 10 (NULL) 3 checked
12 M-12 10 (NULL) 4 checked
12 M-12 14 (NULL) 1 checked
12 M-12 14 (NULL) 2 unchecked
12 M-12 14 (NULL) 3 checked
12 M-12 14 (NULL) 4 checked
12 M-12 15 (NULL) 1 checked
12 M-12 15 (NULL) 2 unchecked
12 M-12 15 (NULL) 3 checked
12 M-12 15 (NULL) 4 checked
12 M-12 (NULL) N/A 1 checked
12 M-12 (NULL) N/A 2 unchecked
12 M-12 (NULL) N/A 3 checked
12 M-12 (NULL) N/A 4 checked
更新
预期结果:
id code monitoring_question_choice_id textual_answer monitoring_checklist_id answer
12 M-12 1 (NULL) 1 checked
12 M-12 4 (NULL) 2 unchecked
12 M-12 6 (NULL) 3 checked
12 M-12 10 (NULL) 4 checked
12 M-12 14 (NULL)
12 M-12 15 (NULL)
12 M-12 (NULL) N/A
任何帮助都将不胜感激。
答案 0 :(得分:2)
由于您正在使用左连接,因此每个ARGV.each do|arg|
# do whatever with arg
end
与monitoring_checklist_id
的给定组合(无重复)
如果你不想要组合,你将不得不以某种方式确定哪个monitoring_question_choice_id
应该monitoring_question_choice_id
(我实际上看不到){/ p>
另一种方法是通过循环所有行并组合而不重复来用编程语言处理它
答案 1 :(得分:0)
谢谢你们的帮助。
我找到了解决这个问题的方法;由于我不想要组合数据,我处理这种情况的方式不是使用LEFT JOIN
,我分别查询每个表并得到我期望的结果。