2016-10-13 88 views
1

如何與多個AND,一個SQL CASE語句結合起來,這樣我可以檢查多個條件,即 我想隨機233改變region244shopid = 455SQL結合案例,並與

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 

我得到這個錯誤:

Operand '233' part of predicate '233 AND shopid = 455' should return type 'BOOLEAN' but returns type 'INT'.

FYI FLOOR(RAND()*(3-1)+1)要麼產生1或2

+2

情況下,當REGION = 233 ............. – BobSki

+0

哪個RDBMS是這個呢?請添加一個標籤來指定您是使用'mysql','postgresql','sql-server','oracle'還是'db2' - 或者其他的東西。 –

回答

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