亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

數據庫高手進 問題神奇

數據庫高手進 問題神奇

夢里花落0921 2018-11-06 05:02:19
這是一個語句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

就沒有問題了


查看完整回答
反對 回復 2018-11-07
  • 2 回答
  • 0 關注
  • 502 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號