2014-07-13 3 views
0

case 문 안에 a 또는 문을 넣은 쿼리가있다.또는 내부 case 문이 mysql에서 실행되지 않는다.

또는 명령문이 어떤 이유로 실행되지 않습니다.

예 - 가시성가 null의 경우

update shouts set visibility=(case when (visibility = null or visibility =1) 
then 2 else 1 end) where shout_id = 788 

지금은 NULL와 비교할 때이 분야 1.

+1

'가시성이 null입니다.'시도하십시오. –

+0

혼란스러운 업데이트는 null이어야하며 검색어는 'null입니다.' –

답변

2

사용 IS 연산자를 업데이트 알려진 바 없음 이유 2 동안 업데이트해야합니다. NULL과 비교하면 은 이 아닌 일반 연산자를 사용할 경우 을 알 수 없으므로이됩니다.

update shouts 
set visibility = case when visibility IS null or visibility = 1 
         then 2 
         else 1 
       end 
where shout_id = 788 
1

난 당신이 논리를 단순화 할 수 있음을 지적하고자 :

update shouts 
    set visibility = (case when visibility <> 1 then 1 else 2 end) 
    where shout_id = 788; 

심지어 :

update shouts 
    set visibility = 2 - (visibility <> 1) 
    where shout_id = 788; 

유르겐의가 있지만, 특정 문제에 적합한 솔루션이다.

+0

첫 번째 코드에 감사드립니다. 두 번째 경우 혼란이 있습니다. 가시성이 2이면 2-2 = 0이고 가시성이 null이면 2가 2로됩니다. –

+0

@PradyutBhattacharya. . . 표현식'visibility <> 1'은 부울 표현식입니다. true이면 '1', false이면 '0'으로 처리됩니다. –