這是一個語句select case cast(rand()*10 as int)%4 when 0 then 0when 1 then 1 when 2 then 2 when 3 then 3 else 4 end這樣執行的結果經常出現一個 4 ?? 為什么啊 這樣寫 就不會出現4 select cast(rand()*10 as int)%4 if(abs(checksum(newid()))%4='')beginprint '123'end if(abs(checksum(rand()))%4='')beginprint '123'end以上2個結果也經常出現 123 為什么了。 把問題發到了首頁,急切想找到答案哦。 問了好多了。還是沒有答案。
2 回答

九州編程
TA貢獻1785條經驗 獲得超4個贊
select
case cast(rand()*10 as int)%4
when 0 then 0
when 1 then 1
when 2 then 2
when 3 then 3
else 4 end
相當于
if (cast(rand()*10 as int)%4 == 0)
//print 0
else if (cast(rand()*10 as int)%4 == 1)
//print 1
else if (cast(rand()*10 as int)%4 == 2)
//print 2
else if (cast(rand()*10 as int)%4 == 3)
//print 3
else
//print 4
每次都產生 了隨機數,所以才會出現4
使用
DECLARE @d int
SET @d = cast(rand()*10 as int)%4
SELECT
case @d
when 0 then 0
when 1 then 1
when 2 then 2
when 3 then 3
else 4 end
就沒有問題了
- 2 回答
- 0 關注
- 502 瀏覽
添加回答
舉報
0/150
提交
取消