我有一张桌子,可以使用计数功能给我一个考试成绩。我需要执行相同的功能来比较test1和test2。
找到第一个考试分数我使用count函数并执行在where子句中添加一些约束,然后按用户名分组,
我需要将此计数与需要在第二个select语句中的第二个计数进行比较,但是我不能在having子句中添加select语句,并且我不能对where子句中的count进行任何操作。以下是我现在所掌握的基本概念
SELECT ur.uno, COUNT(*)*5 as test1
FROM question q,
userresponse ur
WHERE q.eno = '1'
AND q.eno = ur.eno
AND q.qno = ur.qno
AND q.correctanswer = ur.response
AND test1 > (SELECT ur.uno, COUNT(*)*5
FROM question q1,
userresponse ur1
WHERE q1.eno = '3'
AND q1.eno = ur1.eno
AND q1.qno = ur1.qno
AND q1.correctanswer = ur1.response
GROUP BY ur.no)
GROUP BY ur.uno
这是我的第一种方法,但是我得到了test1无效的标识符。我的另一种方法是
select ur.uno, count(*)*5 as test1
from question q, userresponse ur
where q.eno = '1' and q.eno = ur.eno and q.qno = ur.qno and q.correctanswer = ur.response
group by ur.uno
having count(*)*5 > (select ur1.uno, count(*)*5 as test3
from question q1, userresponse ur1
where q1.eno = '3' and q1.eno = ur1.eno and q1.qno = ur.qno and q.correctanswer = ur.response
group by ur1.uno)
但我得到了太多的价值错误,有什么想法吗?
答案 0 :(得分:1)
@ user541597; 嘿从内部查询中删除“ur.uno”我认为它会起作用..
答案 1 :(得分:1)
尝试:
SELECT ur.uno,
COUNT(case when q.eno='1' then 1 end)*5 as test1,
COUNT(case when q.eno='3' then 1 end)*5 as test3
FROM question q,
userresponse ur
WHERE q.eno in ('1', '3')
AND q.eno = ur.eno
AND q.qno = ur.qno
AND q.correctanswer = ur.response
HAVING COUNT(case when q.eno='1' then 1 end) >
COUNT(case when q.eno='3' then 1 end)
答案 2 :(得分:0)
第二个查询中的where子句不识别别名,因此您可以替换test1。
你可以改变
select ur1.uno, count(*)*5 as test3
到
select count(*)*5 as test3
在第二个select语句中,以避免过多的值错误。