SQL结合CASE和AND

时间:2016-10-13 19:04:24

标签: sql

如何将SQL CASE语句与多个AND组合,以便我可以检查多个条件,即 我想将region233随机更改为244 shopid = 455

select
    id,
    case region
       when 233 and shopid = 455 and FLOOR(RAND()*(3-1)+1) = 1 then 244
       when 233 and shopid = 455 and FLOOR(RAND()*(3-1)+1) = 2 then 233
       else region
    end
from 
    table1

我收到此错误:

  

操作数é 233'谓词的一部分&233; AND shopid = 455'应该返回类型' BOOLEAN'但返回类型' INT'。

FYI FLOOR(RAND()*(3-1)+1)将产生1或2

1 个答案:

答案 0 :(得分:6)

使用其他形式的case

select
id,
case 
  when region = 233 and shopid = 455 and FLOOR(RAND()*(3-1)+1) = 1 then 244
  when region = 233 and shopid = 455 and FLOOR(RAND()*(3-1)+1) = 2 then 233
  else region
end
from table1