Mysql 使用UNION时有2个不同的字段
我想在一个结果表中显示两个查询,其中包含两个字段。错误答案和正确答案 错误答案查询:Mysql 使用UNION时有2个不同的字段,mysql,Mysql,我想在一个结果表中显示两个查询,其中包含两个字段。错误答案和正确答案 错误答案查询: select count(ma_id) as wrong_answers from exercicio natural join avaliacao natural join user_sessao where se_id=4 and us_id=1 and not exists ( select ma_id from grelha natural join exercicio natu
select count(ma_id) as wrong_answers
from exercicio natural
join avaliacao natural
join user_sessao
where se_id=4 and us_id=1 and not exists (
select ma_id
from grelha natural
join exercicio natural
join avaliacao natural
join user_sessao
where us_id=1 and se_id=4
)
select count(ma_id) as right_answers
from exercicio natural
join avaliacao natural
join user_sessao natural
join grelha where se_id=4 and us_id=1
右键回答查询:
select count(ma_id) as wrong_answers
from exercicio natural
join avaliacao natural
join user_sessao
where se_id=4 and us_id=1 and not exists (
select ma_id
from grelha natural
join exercicio natural
join avaliacao natural
join user_sessao
where us_id=1 and se_id=4
)
select count(ma_id) as right_answers
from exercicio natural
join avaliacao natural
join user_sessao natural
join grelha where se_id=4 and us_id=1
当我执行错误答案
查询联合正确答案
查询时,两个结果都显示在错误答案列中
如何使它们成为2个不同的列
谢谢
UNION ALL
保留两个结果集中的列。如果一列同时出现在两个查询中,那么它将在最终结果集中出现一次。在这种情况下,您不需要联合查询,如果您希望这些查询位于两个不同的列中,可以使用以下方法:
SELECT NULL AS correct, count(ma_id) AS wrong
.... wrong answer stuff here
UNION ALL
SELECT count(ma_id), NULL
.... right answer stuff here
SELECT
(
select count(ma_id) as wrong_answers
from exercicio
natural join avaliacao
natural join user_sessao
where se_id=4 and us_id=1 and not exists (
select ma_id
from grelha
natural join exercicio
natural join avaliacao
natural join user_sessao
where us_id=1 and se_id=4
)
) AS wrong_answers,
(
select count(ma_id) as right_answers
from exercicio
natural join avaliacao
natural join user_sessao
natural join grelha
where se_id=4 and us_id=1
) AS right_answers;
尝试使用
UNION ALL