创建视图并用单词替换空值

时间:2013-08-24 15:19:46

标签: mysql string view replace null

我有一个视图,我想用一个单词other更改列中的空值。

这是我到目前为止所尝试的:

CREATE VIEW APB1 AS
  SELECT jos_jam.postcode, location_id, description, 
         category, apb.street, location, apb
  FROM jos_jam
  LEFT JOIN apb
  ON jos_jam.postcode=apb.postcode;
  FROM apb
  UPDATE APB1 SET apb = 'Other' where apb is null

没有正确替换值,我也尝试过:

CREATE VIEW APB1 AS
  SELECT jos_jam.postcode, location_id, description, 
         category, apb.street, location, apb
  FROM jos_jam
  LEFT JOIN apb
  ON jos_jam.postcode=apb.postcode;
  REPLACE(apb.apb, 'NULL') as 'Other'
  FROM apb

第二个查询出现以下错误:

#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 apb
UPDATE APBVolunteers4 SET apb = 'Other' where apb is null' at line 1 

此错误的含义是什么?如何在视图中用单词替换空值?

1 个答案:

答案 0 :(得分:1)

错误消息是指第一个查询中单词FROM之前的分号或第二个查询中单词Replace的分号。

您不能在创建视图语句中放置更新语句。看起来您正在尝试更新数据库,而不是更新视图中返回的内容。

此外,您不需要声明底部的“来自APB”。

COALESCE()运算符在其参数列表中返回第一个非空值。

我认为这应该有效:

CREATE VIEW APB1 AS
    SELECT jos_jam.postcode, location_id, description, category, apb.street, 
    location, COALESCE(apb, 'Other') as apb
FROM jos_jam
LEFT JOIN apb
   ON jos_jam.postcode=apb.postcode