2016-05-30 84 views
0

下面的查詢工作正常,是我需要的。 我想在「關鍵字」上添加一個新的關鍵字,例如SQL字段更新語法複雜選擇邏輯的問題

UPDATE bugs SET bug.keywords = CONCAT(bug.keywords, ', Report:DevProcess') 

但是,無論我把它放在下面的邏輯中,我得到一個語法錯誤。

我看到的和在Stackoverflow中的網絡示例適用於簡單的 Update ... WHERE ....示例。

SET @StartDate = '2016-03-01'; 
SET @EndDate = '2016-03-31'; 

SELECT 
    bugs_activity.bug_id, 
    bug.status_whiteboard AS Whiteboard, 
    bug.keywords AS Keywords, 
    bug.bug_status, 
    bug.resolution, 
    SUM(CASE WHEN fd.name = 'bug_status' AND (bugs_activity.added = 'VERIFIED' OR bugs_activity.added = 'CLOSED') THEN 1 ELSE 0 END) AS ClosedCount, 
    MIN(CASE WHEN fd.name = 'bug_status' AND bugs_activity.added = 'VERIFIED' THEN bug_when ELSE NULL END) AS verifiedDate, 
    MIN(CASE WHEN fd.name = 'bug_status' AND bugs_activity.added = 'CLOSED' THEN bug_when ELSE NULL END) AS closedDate 
FROM bugs_activity 
INNER JOIN bugs bug 
    ON bugs_activity.bug_id = bug.bug_id 
INNER JOIN fielddefs fd 
    ON bugs_activity.fieldid = fd.id 
WHERE 
    (bugs_activity.bug_when BETWEEN '2015-09-01' AND @EndDate) 
    AND (Keywords LIKE '%Region:Europe%') 
    AND NOT (Keywords LIKE '%Report:DevProcess%') 
GROUP BY bug_id 
HAVING 
    ClosedCount > 0 
    AND (
     (verifiedDate IS NOT NULL AND verifiedDate >= @StartDate) 
     OR (verifiedDate IS NULL AND (closedDate IS NOT NULL AND closedDate >= @StartDate)) 
    ) 

從問題附加信息: Linqpad SQL說話的MySQL數據庫

From linqpad - 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 'UPDATE bugs SET bug.keywords = CONCAT(bug.keywords, ', Report:DevProcess')' at line 20 

我刪除的GroupBy線,同樣的錯誤。 CONCAT是網絡搜索告訴我How to prepend a string to a column value in MySQL?

在所選擇的 「錯誤」 是我想做的 「UPDATE錯誤SET bug.keywords = CONCAT(bug.keywords」,報告:DevProcess')」

的Bugzilla歷史上長時間使用多個關鍵字。這是好還是壞。

== 31/05/2016更新==

我簡化了查詢,並獲得過去的語法錯誤,但是沒有更新。我通過使用產生拒絕訪問錯誤的讀取帳戶確認帳戶具有數據庫寫入權限。

-- this shows the one record 
SELECT bug_id 
FROM bugs 
WHERE (bugs.bug_status = 'VERIFIED') AND (bugs.status_whiteboard LIKE '%Leiden%') and (bugs.keywords LIKE '%Region:Europe%') AND NOT (bugs.keywords LIKE '%Report:DevProcess%') 

-- this runs without an error but shows no records updated 
UPDATE bugs SET bugs.keywords = CONCAT(bugs.keywords, ', Report:DevProcess') 
WHERE (bugs.bug_status = 'VERIFIED') AND (bugs.status_whiteboard LIKE '%Leiden%') and (bugs.keywords LIKE '%Region:Europe%') AND NOT (bugs.keywords LIKE '%Report:DevProcess%') 
+0

添加complate錯誤消息 – Jens

+0

什麼是'CONCAT'?你使用SQL Server 2012嗎? –

+0

你有'GROUP BY'。您無法使用「GROUP BY」更新語句。 –

回答

0

更簡單的語法是「工作」,即沒有語法錯誤,但是寫入最初不起作用。原來,這是因爲關鍵字模式需要預先定義關鍵字。添加關鍵字導致正在更新的記錄。

-- this runs without an error but shows no records updated 
UPDATE bugs SET bugs.keywords = CONCAT(bugs.keywords, ', Report:DevProcess') 
WHERE (bugs.bug_status = 'VERIFIED') AND (bugs.status_whiteboard LIKE '%Leiden%') and (bugs.keywords LIKE '%Region:Europe%') AND NOT (bugs.keywords LIKE '%Report:DevProcess%')