[英]Using group_concat with update statement in MySql
I am trying to execute the below update query using GROUP_CONCAT
but it is failing.我正在尝试使用GROUP_CONCAT
执行以下更新查询,但它失败了。
Can anyone kindly help me in correcting it, moreover the statement must always start with the “update” keyword is the catch here as well.任何人都可以帮助我更正它,而且声明必须始终以“更新”关键字开头也是这里的问题。
UPDATE firstEntry f,
objects o,
titlement_values e
SET e.customattribute12 = (
SELECT GROUP_CONCAT(DISTINCT o.minvalue)
WHERE f.ldkey = o.ld_key
AND o.TITLE_VALUEKEY = e.TITLE_VALUEKEY
AND e.TITLE_VALUE = 'ZD%'
AND f.ldkey = 13
AND e.TITLEMENTTYPEKEY = 13
GROUP BY e.title_value)
I tried to execute the below query as well but no luck in it as well:我也尝试执行以下查询,但也没有运气:
UPDATE firstEntry f,
objects o,
titlement_values e
SET e.customattribute12 = minval
FROM (SELECT GROUP_CONCAT(DISTINCT o.minvalue) AS minval,
e.title_value
WHERE f.ldkey = o.ld_key
AND o.TITLE_VALUEKEY = e.TITLE_VALUEKEY
AND e.TITLE_VALUE = 'ZD%'
AND f.ldkey = 13
AND e.TITLEMENTTYPEKEY = 13
GROUP BY e.title_value)
Here is the table result of using select statement on joining the 3 tables mentioned in the query这是使用 select 语句连接查询中提到的 3 个表的表结果
group_concat group_concat | e.titlement_value e.titlement_value |
---|---|
A1,A2,A3 A1,A2,A3 | Zd_A Zd_A |
A1,B2 A1,B2 | Zd_B Zd_B |
Now i need to take the value of this group_concat and update it in the column e.customattribute12 as shown现在我需要获取此 group_concat 的值并在列 e.customattribute12 中更新它,如图所示
e.titlement_value e.titlement_value | e.customattribute12 e.customattribute12 |
---|---|
zd_A zd_A | A1,A2,A3 A1,A2,A3 |
zd_B zd_B | A1,B2 A1,B2 |
You need to rewrite your update as MySQL doesn't support FROM
clauses in UPDATE
statements: a correlated subquery will do the trick, as it returns only one scalar value.您需要重写更新,因为 MySQL 不支持UPDATE
语句中的FROM
子句:相关子查询可以解决问题,因为它只返回一个标量值。
Also JOIN
operations are around for 30 years and are established standard, you should switch also to it.此外, JOIN
操作已有 30 年历史,并且已成为标准,您也应该改用它。
with the problem you mentioned in the comment.你在评论中提到的问题。
mjava.sql.BatchUpdateException: You can't specify target table 'e' for update in FROM claus mjava.sql.BatchUpdateException:您无法在 FROM 子句中指定要更新的目标表“e”
it os MySQL error 1063它操作系统 MySQL 错误 1063
MySQL doesn't like when the updated table is in somewhere used again, we can avoid that with a trick seen below, as we force MySql to produce a new temporary table MySQL 不喜欢更新的表在某个地方再次使用,我们可以使用下面的技巧来避免这种情况,因为我们强制 MySql 生成一个新的临时表
UPDATE titlement_values e
SET
e.customattribute12 = (SELECT
GROUP_CONCAT(DISTINCT o.minvalue)
FROM
firstEntry f
INNER JOIN
objects o ON f.ldkey = o.ld_key
INNER JOIN
(SELECT * FROM titlement_values) e1 ON o.TITLE_VALUEKEY = e1.TITLE_VALUEKEY
WHERE
e1.TITLE_VALUE LIKE 'ZD%' AND f.ldkey = 13
AND e1.TITLEMENTTYPEKEY = 13
AND e1.title_value = e.title_value)
Once you are able to make sure that the subquery works correctly, try applying the join between your table to be updated and your crafted subquery, on matching " title_value " values.一旦您能够确保子查询正常工作,请尝试在要更新的表和您精心设计的子查询之间应用连接,以匹配“ title_value ”值。
UPDATE titlement_values
INNER JOIN (SELECT GROUP_CONCAT(DISTINCT o.minvalue) AS minval,
e.title_value
FROM firstEntry f
INNER JOIN objects o ON f.ldkey = o.ld_key
INNER JOIN titlement_values e ON o.TITLE_VALUEKEY = e.TITLE_VALUEKEY
WHERE e.TITLE_VALUE LIKE 'ZD%'
AND f.ldkey = 13
AND e.TITLEMENTTYPEKEY = 13 ) cte
ON titlement_values.title_value = cte.title_value
SET customattribute12 = cte.minval
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.