我想在一个包含2个字段的结果表中显示2个查询。 wrong_answers和right_answers。
wrong_answers查询:
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
)
right_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
当我wrong_answers
查询UNION right_answers
时,查询结果会显示在wrong_answers列中。
如何让它们成为2个差异列?
谢谢
答案 0 :(得分:2)
在这种情况下,您不需要联合查询,如果您希望这些查询位于两个不同的列中,您可以使用:
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;
答案 1 :(得分:0)
SELECT NULL AS correct, count(ma_id) AS wrong
.... wrong answer stuff here
UNION ALL
SELECT count(ma_id), NULL
.... right answer stuff here
答案 2 :(得分:0)
UNION ALL
保留两个结果集中的列。如果两个查询中都有一列,那么它将在最终结果集中出现一次。