[英]How to use subselection result as temp table in MySQL update query?
table A
uid
uname
temp table B
uid
uname
表B不是真正的表,而是子查詢的結果,例如
select uid, uname
from tableC left join tableD on tableC.pid = tableD.pid
where tableC.qid = tableD.qid
group by uid;
我想更新集合A.uname = B.uname,其中A.uid = B.uid
我該如何使用MySQL?
將子查詢視為JOIN
的表:
UPDATE tableA AS a
JOIN (select uid, uname
from tableC
left join tableD on tableC.pid = tableD.pid and tableC.qid = tableD.qid
group by uid) AS b ON a.uid = b.uid
SET a.uname = b.uname
另外,請注意,在LEFT JOIN
,引用第二個表的所有條件都應在ON
子句中。 否則,您將過濾掉所有不匹配的行,因為這些列的值將為NULL
,這將否定使用LEFT JOIN
而不是INNER JOIN
。
update a
set uname = B.name
from A a
inner join (
select uname [name]
from tableC left join tableD on tableC.pid = tableD.pid
where tableC.qid = tableD.qid
group by uid ) b
ON a.uid = b.uid
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.