您好,我试图通过加入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.
发布评论
评论(2)
您的数据
使用
完整的JOIN
在mysqlyour data
use
full join
equivalent in mysqldbfiddle
尝试以下操作:
Try this: