1

我有以下UPDATE声明:

update MESSAGES set status=
    (select (CASE 
               WHEN from_id='111111111' 
               THEN (CASE 
                      WHEN status='A' 
                      THEN 'S' 
                  WHEN status='R' 
                      THEN 'D' 
                     END)
               WHEN to_id='111111111' 
               THEN (CASE 
                       WHEN status='A' 
                       THEN 'R' 
                   WHEN status='S' 
                       THEN 'D' 
                     END) 
             END)
    as status_value)
where primary_key='236499681204' 
and status_value not null ;

此查询的问题是,在最后一行,status_value无法识别。如果我删除最后一个比较and status_value not null,它会起作用。我不希望上面的版本可以工作,但是有人可以告诉我一个可以达到同样效果的替代方案吗?

有没有办法可以AS在查询中使用关键字UPDATE来定义变量status_value

编辑

实际要求不仅是我检查是否status_valuenull,而且如果是,我执行任何更新null

4

3 回答 3

3

尝试添加ELSE STATUS和删除and status_value not null

  update MESSAGES set status=
    (select (CASE 
               WHEN from_id='111111111' 
               THEN (CASE 
                      WHEN status='A' 
                      THEN 'S' 
                  WHEN status='R' 
                      THEN 'D' 
                     END)
               WHEN to_id='111111111' 
               THEN (CASE 
                       WHEN status='A' 
                       THEN 'R' 
                   WHEN status='S' 
                       THEN 'D' 
                     END) 
             ELSE STATUS 
             END)
    as status_value)
where primary_key='236499681204'; 

您也可以尝试使用view

create view MyView as
select (CASE 
               WHEN from_id='111111111' 
               THEN (CASE 
                      WHEN status='A' 
                      THEN 'S' 
                  WHEN status='R' 
                      THEN 'D' 
                     END)
               WHEN to_id='111111111' 
               THEN (CASE 
                       WHEN status='A' 
                       THEN 'R' 
                   WHEN status='S' 
                       THEN 'D' 
                     END) 
             END)
    as status_value;

update MESSAGES set status= mv.status_value
from MyView mv
where primary_key='236499681204'
and mv.status_value is null

或者你可以使用case in condition

 update MESSAGES set status=
    (select (CASE 
               WHEN from_id='111111111' 
               THEN (CASE 
                      WHEN status='A' 
                      THEN 'S' 
                  WHEN status='R' 
                      THEN 'D' 
                     END)
               WHEN to_id='111111111' 
               THEN (CASE 
                       WHEN status='A' 
                       THEN 'R' 
                   WHEN status='S' 
                       THEN 'D' 
                     END) 
             END)
    as status_value)
where primary_key='236499681204'
and CASE
     WHEN from_id='111111111' 
      THEN (CASE 
             WHEN status='A' 
             THEN 'S' 
             WHEN status='R' 
             THEN 'D' 
            END)
     WHEN to_id='111111111' 
      THEN (CASE 
             WHEN status='A' 
             THEN 'R' 
             WHEN status='S' 
             THEN 'D' 
            END) 
    END is not null; 
于 2012-10-03T21:03:24.080 回答
1

在我看来,您正在尝试对 update 语句进行子查询,而我认为这并不是真正必要的。

您可以像这样简化查询:

UPDATE MESSAGES
SET status = CASE
         WHEN 
            (from_id='111111111' AND status='A') THEN 'S'
         WHEN 
            (to_id  ='111111111' AND status='A') THEN 'R'
         WHEN 
            (from_id  ='111111111' AND status='R') OR
            (to_id  ='111111111' AND status='S') THEN 'D'

         ELSE status
         END
WHERE priamry_key = '236499681204'
于 2012-10-03T21:04:57.457 回答
1

假设你想要的只是摆脱空值,你可以这样做:

update MESSAGES set status=
    IFNULL(
    (CASE WHEN from_id='111111111' THEN 
        (CASE WHEN status='A' THEN 'S' 
        WHEN status='R' THEN 'D' END)
    WHEN to_id='111111111' THEN 
        (CASE WHEN status='A' THEN 'R' 
        WHEN status='S' THEN 'D' END) END),
    status)
where primary_key='236499681204' ;
于 2012-10-03T21:09:10.390 回答