2014-07-22 41 views
0

是:在輸出獲得複製在我有4表DAT MySQL查詢

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 
+1

無法顯示您所期待的MySQL的方式,這不是MySQL的,而它的應用層面,應照顧這的工作,但你能做的就是用'GROUP_CONCAT(選項)'同時選擇數據,然後'按問題分組' –

+0

可以向我顯示查詢嗎? bcz m不知道這個 – user3831519

+0

'選擇q.Question,group_concat(o.Options)作爲選項...... qp.Test_Id = 9 group by q.Question' –

回答

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 

DEMO

+0

錯誤即將在你的查詢 – user3831519

+0

我試圖在sqlfiddle運行你的查詢,但我得到很多錯誤,因爲你有不同的表和列名稱在查詢比你在表格列表中。沒有'Question_Type_Master'表(它只是'Question_Type',並且沒有'questiontype'列,它是'question_type'。修復所有這些錯誤令我瘋狂。 – Barmar

+0

'option'是一個保留字,它必須反引號。我糾正了所有其他的命名錯誤,而且我必須刪除'test_id = 9',因爲這不在示例數據中。請參閱工作sqlfiddle。 – Barmar

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 
+0

那條件你寫了o.option ='a'那不是固定的.option可以是任何東西 – user3831519

+0

@ user3831519現在檢查它 – Sathish

+0

很多錯誤即將到來。你有沒有檢查過sql提琴?] – user3831519