SQL字段更新语法复杂选择逻辑的问题

时间:2016-05-30 06:11:03

标签: mysql sql tsql

下面的查询工作正常,是我需要的。 我想在“关键字”中添加新关键字,例如

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

但无论我将其放在下面的逻辑中,我都会遇到语法错误。

我在Weboverflow中看到的Web示例非常简单 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 DB交谈

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?

在选定的“错误”中,我想“更新错误SET bug.keywords = CONCAT(bug.keywords,',Report:DevProcess')”

Bugzilla历来长期使用多个关键字。这是好的还是坏的。

== 31/05/2016 update ==

我简化了查询并超出了语法错误,但没有更新。我通过使用读取帐户确认帐户具有数据库写入访问权限,该帐户产生访问被拒绝错误。

-- 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%') 

1 个答案:

答案 0 :(得分: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%')