数据库:tennis
,即网球俱乐部。
讨论的表格:penalties
列:paymentno
,amount
,playerno
任务:
如何在不使用计数功能的情况下执行第2部分?这可能吗?
1的SQL查询:
use tennis;
select tennis.penalties.paymentno,
tennis.penalties.amount,
tennis.penalties.playerno,
case
when playerno >= 0 and playerno <= 40 then 'low'
when playerno > 40 and playerno < 80 then 'medium'
when playerno > 80 then 'high'
end
from tennis.penalties;
感谢。
答案 0 :(得分:1)
SUM(playerno >= 0 and playerno <= 40) AS count_penalties_in_low
或
SUM(CASE WHEN playerno >= 0 and playerno <= 40 THEN 1 ELSE 0 END) AS count_penalties_in_low
从技术上讲,你总结了1,实际上等于计数
PS:
playerno >= 0 and playerno <= 40
可以改写为
playerno BETWEEN 0 AND 40
PPS:
playerno
= 80不受任何条件的限制
PPPS:我会用这种方式写这个案子:
case
when playerno <= 40 then 'low'
when playerno <= 80 then 'medium'
else 'high'
end
PPPPS:没有功能的解决方案(概念)
SELECT @I:=@I+1, other_columns FROM table, (SELECT @I:=0) x
并且拥有@I你可以计算你想要的东西
但在这种情况下,这是一个可怕的解决方案