我有4个表格数据是:
Question_Paper_Master:此表包含为测试(考试)选择的问题的问题ID。
Question_Paper_Id Question_Id Test_Id
1 1 1
2 2 1
3 3 1
4 4 1
Question_Bank:此表包含问题列表。
Question__Id Question Question_Type_id
1 abc 1
2 pqr 1
3 lmn 1
4 xyz 1
Question_Type:此表包含问题dat的类型是单选(单选按钮)或多选(复选框)。
Question__type_id Question_type
1 single choice
2 multiple choice
Option_Master:此表包含问题选项列表。
option__Id Question_id option
1 1 a
2 1 b
3 1 c
4 1 d
.
.
.
现在的问题是,我想逐个获取选项及其类型(单个或多个),并在数据表中获取输出。
我的查询如下:
select q.Question,o.Options,t.Question_Type
from Question_Paper_Master Qp,Question_Bank Q,Option_Master o,Question_Type_Master T
where qp.Question_Id=q.Question_Id and q.Question_Id=o.Question_Id
and q.Question_Type_Id=t.Question_Type_Id
and qp.Test_Id=9
但输出结果如下:
question option questiontype
abc a singlechoice
abc b single choice
abc c singlechoice
abc d single choice
pqr a singlechoice
pqr b single choice
pqr c singlechoice
pqr d single choice
我想要这样的输出:
question option questiontype
abc a singlechoice
b
c
d
pqr a singlechoice
b
c
d
答案 0 :(得分:0)
试试这个
select case when rank=1 then question else "" end "question",
`Option`,case when rank=1 then question_type else "" end "type"
from (
select q.question,o.`Option`,t.question_type,
(
CASE question
WHEN @curType
THEN @curRow := @curRow + 1
ELSE @curRow := 1 AND @curType := question END
) + 1 AS rank
from Question_Paper_Master Qp,Question_Bank Q,Option_Master o,Question_Type T,
(SELECT @curRow := 0, @curType := '') r
where qp.Question_Id=q.Question_Id and
q.Question_Id=o.Question_Id
and q.Question_Type_Id=t.Question_Type_Id
and qp.Test_Id=1) as t
答案 1 :(得分:0)
SELECT question, `option`, questiontype
FROM (
SELECT IF(question = @prev_question, '', question) AS question,
`option`,
IF(question = @prev_question and question_type = @prev_type, '', question_type) AS questiontype,
@prev_question := question, @prev_type := question_type
FROM (select q.Question,o.`Option`,t.Question_Type
from Question_Paper_Master Qp,Question_Bank Q,Option_Master o,Question_Type T
where qp.Question_Id=q.Question_Id and q.Question_Id=o.Question_Id
and q.Question_Type_Id=t.Question_Type_Id
ORDER BY question, question_type) x
CROSS JOIN (SELECT @prev_question := null, @prev_type := null) vars
) subq