当前位置:网站首页>A solution to the problem that MySQL cannot update the query result set

A solution to the problem that MySQL cannot update the query result set

2022-06-09 14:31:00 dmhxm

1、update The table and... Of the subquery contained in the update When the table of is the same table , Report errors

--  Wrong cases 
UPDATE tab_user 
SET sex = ' Woman ' 
WHERE
	uid IN (
	SELECT
		uid 
	FROM
		tab_user 
WHERE
	STATUS = 'N')
--  Report errors :You can't specify target table 'tab_user' for update in FROM clause

--  terms of settlement 
--  Nest the subquery result set one level select And take an alias to solve 
UPDATE tab_user 
SET sex = ' Woman ' 
WHERE
	uid IN (
	SELECT
		uid 
FROM
	( SELECT uid FROM tab_user WHERE `status` = 'N' ) AS temp)

2. When you need to modify the result set of a linked table query, you will be prompted that you cannot update Result set

--  Solution 
--  Modify table join query table , direct update Modify the table 
UPDATE A surface  AS i
RIGHT JOIN (
	SELECT
		b.* 
	FROM
		A surface  a
		INNER JOIN B surface  b ON a.studentcode = b.CODE 
	WHERE
		a.schoolitemcode != b.schoolitemcode 
	) AS s ON i.studentcode = s.CODE 
	SET i.schoolitemcode = s.schoolitemcode
原网站

版权声明
本文为[dmhxm]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206091258055797.html