【问题标题】:SQL Field Update Syntax Issues with complex selection logic具有复杂选择逻辑的 SQL 字段更新语法问题
【发布时间】:2016-05-30 06:11:03
【问题描述】:

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

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 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?

在选中的“bugs”中我想“UPDATE bugs SET bug.keywords = CONCAT(bug.keywords, ', Report:DevProcess')”

长期以来,Bugzilla 一直使用多个关键字。不管好坏都是如此。

== 2016 年 5 月 31 日更新 ==

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

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

【问题讨论】:

  • 添加编译错误信息
  • 什么是CONCAT?您使用 SQL Server 2012 吗?
  • 你有GROUP BYGROUP BY 不能有更新语句。
  • 您能否发布完整的更新声明。您提供的更新查询似乎是静态的。您用于更新的 select 语句中的哪些列?
  • 如果您明确使用mysql,为什么还要用sql-server 标记它?但无论如何,您都不应该将逗号分隔的值存储在单个列中。

标签: mysql sql tsql


【解决方案1】:

更简单的语法确实“起作用”,即没有语法错误,但写入最初不起作用。原来这是因为关键字模式需要预先定义关键字。添加关键字导致记录被更新。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-08
    • 2013-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多