在mysql查询中获取输出中的重复
我有4个表dat是: 问题\论文\主题:此表包含为testexam选择的问题的问题id在mysql查询中获取输出中的重复,mysql,Mysql,我有4个表dat是: 问题\论文\主题:此表包含为testexam选择的问题的问题id Question_Paper_Id Question_Id Test_Id 1 1 1 2 2 1 3 3 1 4 4 1 问题库
Question_Paper_Id Question_Id Test_Id
1 1 1
2 2 1
3 3 1
4 4 1
问题库:此表包含问题列表
Question__Id Question Question_Type_id
1 abc 1
2 pqr 1
3 lmn 1
4 xyz 1
option__Id Question_id option
1 1 a
2 1 b
3 1 c
4 1 d
.
.
.
问题类型:此表包含问题dat的类型为单选按钮或多选复选框
Question__type_id Question_type
1 single choice
2 multiple choice
选项\主选项:此表包含问题选项列表
Question__Id Question Question_Type_id
1 abc 1
2 pqr 1
3 lmn 1
4 xyz 1
option__Id Question_id option
1 1 a
2 1 b
3 1 c
4 1 d
.
.
.
现在的问题是,我想按问题获取选项及其类型Single或multiple,并在datatable中获取输出
我的问题如下:
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
像这样试试
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
你不能显示你所期望的mysql的方式,这不是mysql的工作,而是应用程序级别,应该考虑到这一点,但是你所能做的就是在选择数据时使用group_concatoption,然后按问题分组。你能用查询向我展示吗??bcz我不知道这个问题,分组选项作为选项……qp.Test\u Id=9按q分组问题你能给我看一下整个查询吗?你写的条件o.option='a'不是固定的。选项可以是anything@user3831519现在检查是否出现错误。您是否签入了sql FIDLE?]我正在尝试在SQLFIDLE中运行您的查询,但是我得到了很多错误,因为查询中的表和列名与表列表中的不同。没有疑问类型主表,它只是疑问类型,没有疑问类型列,它是疑问类型。修复所有这些错误让我发疯。选项是一个保留字,必须在backticks中。我已经纠正了所有其他命名错误。我必须删除test_id=9,因为它不在样本数据中。看到正在运行的sqlfiddle.bt了吗?这个查询会在sql server r2中运行吗?我不知道sql server,我使用的是MySQL。