SQL - 在UPDATE语句中提供AS关键字

时间:2012-10-03 20:54:23

标签: mysql sql

我有以下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

3 个答案:

答案 0 :(得分: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; 

答案 1 :(得分:1)

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

您可以像这样简化查询:

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'

答案 2 :(得分:1)

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

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' ;