显示任何值而不是null?

时间:2012-07-05 21:29:32

标签: mysql null

  • 数据库:网球

  • 表:玩家

  • 部分专栏:playerno,名字,姓氏,leagueno。

  • 任务:如果联盟号码为NULL,则给出值1。

  • 问题:我们可以在没有合并功能的情况下这样做吗?或没有任何其他功能?

  • 我的代码错了。我仍然看到null而不是1.此外,由于这种情况,还有不必要的列。

代码:

use tennis;
select playerno, name, initials,leagueno,
case 
when leagueno = null then 1 
end
from players
where tennis.players.town = 'Stratford'
order by leagueno desc;

请帮我正确地做。我有一个使用合并的答案。但我想尝试另一种方法。

1 个答案:

答案 0 :(得分:1)

我认为你想要的是:

use tennis;

select playerno, name, initials,
case 
when leagueno is null then 1 -- note: is null instead of = null
else leagueno
end as leagueno -- This names the result column "leagueno", which may be useful
                -- depending on how you read the result
from players
where tennis.players.town = 'Stratford'
order by leagueno desc;

这基本上是最后一列leagueno,但如果它是NULL,则会得到1

相关问题