2015-12-30 172 views
-1

SQL查询SQL查询空值插入

SELECT  respondant.respondant_firstname  as first_name, 
      question.question_id    as question_id,           
      answer.answer_id, 
      answer.answer_text, 
      answer.answer_rate, 
      answer.answer_nps, 
      question_radio.question_radio_text as opt 
FROM  question 
LEFT JOIN answer   on answer.answer_question_id = question.question_id 
LEFT JOIN question_radio on answer.answer_question_radio_id = question_radio.question_radio_id 
LEFT JOIN respondant  on answer.answer_respondant_id = respondant.respondant_id 
WHERE  question.question_feedback_id = 1           
ORDER BY question.question_id, answer.answer_id 

输出:

first_name question_id answer_id answer_text answer_rate answer_nps opt 
RM   1   1      5   NULL  NULL 
Y    1   3      0   NULL  NULL 
Ben   1   5      0   NULL  NULL 
akash   1   8     2.5  NULL  NULL 
RM   2   2      0   4  NULL 
Y    2   4      0   3  NULL 
Ben   2   6      0   0  NULL 
akash   2   9      0   0  NULL 
Ben   3   7   Thanks  0   0  NULL 
akash   3   10     0   0  NULL 

我需要的输出:

first_name question_id answer_id answer_text answer_rate answer_nps opt 
RM   1   1      5   NULL  NULL 
Y    1   3      0   NULL  NULL 
Ben   1   5      0   NULL  NULL 
akash   1   8     2.5  NULL  NULL 
RM   2   2      0   4  NULL 
Y    2   4      0   3  NULL 
Ben   2   6      0   0  NULL 
akash   2   9      0   0  NULL 
RM   3   NULL     0   0  NULL 
Y    3   NULL     0   0  NULL 
Ben   3   NULL  ThankS 0   0  NULL 
akash   3   NULL     0   0  NULL 

凡在第三和第四行从去年可是没有的ID 3但我需要更换为3和其他值在这两行必须为空

+2

请用您正在使用的数据库标记您的问题。另外,为什么你在这些行中丢失了'answer_id'信息? –

回答

0

你想做的事情相当复杂。您需要为每个响应者和问题设置一行,然后填写响应者的详细信息。我的猜测是你想要answer_id填入,如果可用。也就是说,对于“Ben”和“Akash”,你想要的结果应该有answer_id

以下查询使用响应者和问题之间的cross join生成所有行。然后它会带来附加信息:

SELECT r.respondant_firstname as first_name, q.question_id as question_id,           
     a.answer_id, a.answer_text, a.answer_rate, a.answer_nps, 
     qr.question_radio_text as opt 
FROM respondant r CROSS JOIN 
    question q LEFT JOIN 
    answer a 
    on a.answer_question_id = q.question_id and 
     a.answer_respondant_id = r.respondant_id LEFT JOIN 
    question_radio qr 
    on a.answer_question_radio_id = qr.question_radio_id    
WHERE q.question_feedback_id = 1           
ORDER BY q.question_id, a.answer_id; 
+0

感谢您的帮助的人 我需要为每个响应和问题,这是 –

+0

我需要行对每个响应者和质疑 行必须是这样的: RM 1个 Y 1 奔1 阿民族军1 RM 2 ÿ 2名 奔2名 阿民族军2 RM 3 Y 3 奔3 阿卡什3应该是这个顺序 –

+0

这就是'respondant [R CROSS JOIN问题q'的作用:它给人们和问题的每个组合。然后,'ORDER BY q.question_id'负责给出所有'1'问题,然后是所有'2'问题等。 – EvilBob22