SQL加入我想要的双列

发布于 2025-02-03 22:10:13 字数 1357 浏览 3 评论 0 原文

您好,我试图通过加入2个视图来创建新的视图。我有1个视图,充满了一个问题ID和正确答案的数量。然后另一个带有错误答案的数量。我想将其合并,以便它只是Question_ID,CRORKE_ANSWER,forgation_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;

这就是我得到的 在此处输入图像说明

这是正确答案的内容 在此处输入图像说明

这是错误答案的内容 在此处输入图像描述

任何帮助都将不胜感激。为了添加我仍然是加入内容的新手,所以我可能会写错误的语法。谢谢

编辑: 两个答案都解决了,谢谢大家 推理我的视图是为了以防万一我需要使用纯粹或错误的数据,我只能使用视图而不是进行完整选择。因为将来我会将该视图与另一表中的数据相结合。

Hello I was trying to make a new view by Joining 2 View. I have 1 view filled with a question id and the number of correct answer. Then another with the number of wrong answer. I want to have it merged so its just question_id, correct_answer, wrong_answer.
Im using MySQL workbench to do my queries.

This the query for making the correct answer view

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;

This is the query for making the wrong answer view

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;

This is the query i use to join both of them

SELECT * FROM v_question_history_correct JOIN v_question_history_false 
    ON v_question_history_correct.question_id = v_question_history_false.question_id;

This is What i get
enter image description here

This is the content of correct answer
enter image description here

This is the content of wrong answer
enter image description here

Any help would be appreciated. To add Im still new to the JOIN stuff so I might have written a wrong syntax to begin with. Thanks

Edit:
Both of the answer solved it thank you all
Reasoning i make View is just in case I need to use the data that has just true or false i can just use the view instead of doing full select. Because in the future i will combine that view with data from another table.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

厌味 2025-02-10 22:10:13

您的数据

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);

使用完整的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;

your data

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);

use full join equivalent in 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;

dbfiddle

凉薄对峙 2025-02-10 22:10:13

尝试以下操作:

    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;

Try this:

    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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文