您好,我試圖通過加入 2 View 來創建新視圖。我有 1 個視圖,其中包含問題 ID 和正確答案的數量。然后另一個與錯誤答案的數量。我想讓它合并,所以它只是 question_id、correct_answer、error_answer。我使用 MySQL 作業臺來做我的查詢。
這是用于制作正確答案視圖的查詢
CREATE VIEW v_question_history_correct
AS
SELECT question_id, COUNT(correctness) AS true_answer FROM skripsi_database_soal.questions_history
WHERE correctness = 'true'
GROUP BY question_id;
這是生成錯誤答案視圖的查詢
CREATE VIEW v_question_history_false
AS
SELECT question_id, COUNT(correctness) AS false_answer FROM skripsi_database_soal.questions_history
WHERE correctness = 'false'
GROUP BY question_id;
這是我用來加入它們的查詢
SELECT * FROM v_question_history_correct JOIN v_question_history_false
ON v_question_history_correct.question_id = v_question_history_false.question_id;
這是我得到的 在這里輸入影像描述
這是正確答案的內容 在 此處輸入影像描述
這是錯誤答案的內容 在 此處輸入影像描述
任何幫助,將不勝感激。添加我對 JOIN 的東西還是新手,所以我可能一開始就寫了錯誤的語法。謝謝
編輯:兩個答案都解決了,謝謝大家我制作視圖的推理是為了以防萬一我需要使用只有真或假的資料,我可以只使用視圖而不是全選。因為將來我會將該視圖與另一個表中的資料結合起來。
uj5u.com熱心網友回復:
你的資料
CREATE TABLE v_question_history_correct(question_id int ,true_answer int);
insert into v_question_history_correct
(question_id,true_answer) VALUES
(5,7),
(6,8),
(7,8);
CREATE TABLE v_question_history_false(question_id int ,false_answer int);
insert into v_question_history_false
(question_id,false_answer ) VALUES
(2,7),
(1,7),
(6,1),
(7,1);
在mysql中使用full join等價物
SELECT VC.question_id,true_answer ,false_answer
FROM v_question_history_correct VC
LEFT JOIN v_question_history_false VF
ON VC.question_id = VF.question_id;
UNION
SELECT VF.question_id,true_answer ,false_answer
FROM v_question_history_correct VC
RIGHT JOIN v_question_history_false VF
ON VC.question_id = VF.question_id;
小提琴手
uj5u.com熱心網友回復:
嘗試這個:
SELECT coalesce(v_question_history_correct.question_id,f.question_id,v_question_history_false.question_id) 'question_id',true_answer,false_answer
FROM v_question_history_correct
full JOIN v_question_history_false ON v_question_history_correct.question_id = v_question_history_false.question_id;
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/484581.html
上一篇:MySQL從多個表中獲取資料
