使用case语句(MYSQL)基于另一个表中的值更新一个表

时间:2014-02-20 16:50:30

标签: mysql

我正在尝试运行以下查询以使用case语句基于另一个表中的值更新表。我收到以下错误。

Error Code: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from tbl_a as a, tbl_b as b
  WHERE a.DIV_NO = b.' at line 12

以下是查询:

UPDATE a
  SET
    a.MIN_5 = b.Min_Five,
    a.IN_PR = b.Ad_Flag ,
    a.FRC = (
                    CASE
                       WHEN b.Prc_Fl = 'N' THEN 'Y'
                       WHEN b.Prc_Fl = 'Y' THEN 'N'
                       ELSE 'D'
                    END
            )
  FROM tbl_a AS a, tbl_b AS b
  WHERE a.DIV_NO = b.Div_num
    AND a.OFR_ID = b.Flg ;

1 个答案:

答案 0 :(得分:2)

使用JOIN

尝试此操作
UPDATE 
  tbl_a AS a
  JOIN   
  tbl_b AS b ON (a.DIV_NO = b.Div_num AND  a.OFR_ID = b.Flg) 
  SET
  a.MIN_5 = b.Min_Five,
  a.IN_PR = b.Ad_Flag,
  a.FRC = 
    CASE
      WHEN b.Prc_Fl = 'N' 
      THEN 'Y' 
      WHEN b.Prc_Fl = 'Y' 
      THEN 'N' 
      ELSE 'D' 
    END