SQL加入我想要的列的两倍
SQL join have a double the column i want
您好,我正在尝试通过加入 2 个视图创建一个新视图。我有 1 个视图,其中包含问题 ID 和正确答案的数量。然后另一个与错误答案的数量。我想合并它,所以它只是 question_id、correct_answer、wrong_answer。
我正在使用 MySQL workbench 进行查询。
此为查看正确答案的查询
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;
这就是我得到的
enter image description here
这是正确答案的内容
enter image description here
这是错误答案的内容
enter image description here
如有任何帮助,我们将不胜感激。添加我对 JOIN 内容还是新的,所以我可能一开始就写了错误的语法。谢谢
编辑:
两个答案都解决了谢谢大家
我制作 View 的原因是为了以防万一我需要使用只有 true 或 false 的数据我可以只使用视图而不是完整 select。因为将来我会将该视图与另一个 table.
的数据结合起来
试试这个:
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;
您的数据
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);
使用 full join
相当于 mysql
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;
您好,我正在尝试通过加入 2 个视图创建一个新视图。我有 1 个视图,其中包含问题 ID 和正确答案的数量。然后另一个与错误答案的数量。我想合并它,所以它只是 question_id、correct_answer、wrong_answer。 我正在使用 MySQL workbench 进行查询。
此为查看正确答案的查询
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;
这就是我得到的 enter image description here
这是正确答案的内容 enter image description here
这是错误答案的内容 enter image description here
如有任何帮助,我们将不胜感激。添加我对 JOIN 内容还是新的,所以我可能一开始就写了错误的语法。谢谢
编辑: 两个答案都解决了谢谢大家 我制作 View 的原因是为了以防万一我需要使用只有 true 或 false 的数据我可以只使用视图而不是完整 select。因为将来我会将该视图与另一个 table.
的数据结合起来试试这个:
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;
您的数据
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);
使用 full join
相当于 mysql
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;