我有一个 View ,我想用一个词更改列中的空值: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
此错误是什么意思,如何在 View 中用单词替换空值?
最佳答案
错误信息指的是第一个查询中的单词 FROM 或第二个查询中的单词 Replace 之前的分号。
您不能将更新语句放在创建 View 语句中。看起来您是在尝试更新数据库,而不是更新 View 中返回的内容。
此外,您的声明底部也不需要 From 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
关于mysql - 创建 View 并用单词替换空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18420070/