[英]SQL update statement is executing but updating with incorrect values
我正在嘗試使用其他表中的值更新我的表之一。 update語句將執行,但不會使用正確的值更新該列。
例如,
這是table1
,我要從table2
更新列SID
這是table2
,其中列SID
包含我需要的值。
我正在嘗試完成的是從uID
, NO
等於且table1.NO
不為null的table2
更新table1
這是我的方法:
update c1 set c1.SID = c2.SID
from table1 c1, table2 c2
where c1.UID = '09999138181408374834'
and c1.NO = c2.NO
and c1.NO is not null
它更新5行,但這是輸出:
table2
table1
中的那些更新的SID與table2
SID不同
關於它為什么發生以及如何解決的任何建議?
通過驗證此查詢返回更新的正確元組來啟動調試過程:
select
c1.NO as NO_target
,c2.NO as NO_source
,c1.SID as SID_old
,c2.SID as SID_new
from table1 c1
join table2 c2
on c1.NO = c2.NO
and c1.UID = c2.UID -- I believe this line is needed to prevent a partial Cartesian product
where c1.UID = '09999138181408374834'
and c1.NO is not null
一旦以上查詢返回正確的元組進行更新,更正更新的過程就很簡單。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.