我有两张桌子:
DistrictMaster :
-----------------------------------------------------
DistrictID | DistrictName | State
-----------------------------------------------------
21 | ABC | ktm
22 | XYZ | ktm
32 | PQR | johar
-----------------------------------------------------
Districtdetail :
District | DistrciID | State | Country
------------------------------------------------
PQR | | johar | MY
null | | Kedah | MY
ABC | | ktm | MY
| | Kedah | MY
XYZ | | ktm | MY
------------------------------------------------
我需要在DistrictID
中插入Districtdetail
,这些insert into Districtdetail(DistrictID)
select
a.id
from
DistrictMaster a
left join
Districtdetail b on a.DistrictName = b.DistrictName
and a.State = b.State;
几乎没有值,也没有值。我在查询下面使用但不插入。
uint8_t digest[CC_SHA1_DIGEST_LENGTH];
NSData* data = [stringToHash dataUsingEncoding:NSUTF8StringEncoding];
char* sha = CC_SHA1(data.bytes, data.length, digest);
NSData *hashedData = [NSData dataWithBytes:sha length:CC_SHA1_DIGEST_LENGTH];
以上查询说已执行但未插入任何内容。
答案 0 :(得分:4)
这是带join的简单更新语句:
update d
set DistrictID = m.DistrictID
from DistrictMaster m
join Districtdetail d on m.DistrictName = d.District and m.State = d.State
答案 1 :(得分:0)
如果连接中未保留主键,则可能会遇到连接更新问题。
在这种情况下,您应该尝试以这种方式更新Districtdetail
:
update Districtdetail dd
set dd.DistrictID = (select dm.DistrictID
from DistrictMaster dm
where dm.DistrictName = dd.District
and dm.State = dd.State)
where exists
(select *
from DistrictMaster dm
where dm.DistrictName = dd.District
and dm.State = dd.State)