2013-08-24 35 views
1

我有一個視圖,我想用一個單詞更改列中的空值: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

該錯誤消息是指字之前分號FROM在所述第一查詢或單詞中的第二查詢替換。

您不能在創建視圖語句中放置更新語句。這看起來像你試圖更新數據庫,而不是更新視圖中返回的內容。

此外,您不需要在聲明底部的來自APB。

聚結()運算符返回其參數列表中的第一個非空值。

我認爲這應該工作:

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