请教一条SQL语句。
这样两张表
T_ADD (具体地址)
ADDRESS ADDRESS_CODE
北京路123号45室 001
上海路789弄01号 002
D_ROAD
MC CODE
北京路 001
上海路 002
现在问题是address_code目前没数据 需要根据 D_ROAD这张代码表 对应T_ADD地址表中的详细地址 更新ADDRESS_CODE的内容 该怎么写呀
前面在SQL 版提问过 发现在ORACLE里面执行会报错。
[解决办法]
update T_ADD t1 set
ADDRESS_CODE = (select t2.CODE
from D_ROAD t2
having subStr(t1.ADDRESS,0,3)=t2.MC
)
where exists (select 'X ' from D_ROAD t3
having subStr(t1.ADDRESS,0,3)=t3.MC)
[解决办法]
T_ADD (具体地址)
ADDRESS ADDRESS_CODE
北京路123号45室 001
上海路789弄01号 002
D_ROAD
MC CODE
北京路 001
上海路 002
update t_add a
set a.address_code = (select code from d_road b where a.address like '% ' || b.mc || '% ')
[解决办法]
update T_ADD t
set ADDRESS_CODE=
(select v.CODE from D_ROAD v where v.mc=substr(t.ADDRESS,0,3))
[解决办法]
update t_add a
set a.address_code = (select code from d_road b where a.address like '% ' || b.mc || '% '),大家想一想,如果原来address_code有值,如果采用这样更新会把有些值更新为NULL值,
[解决办法]
T_ADD (具体地址)
ADDRESS ADDRESS_CODE
北京路123号45室 001
上海路789弄01号 002
D_ROAD
MC CODE
北京路 001
上海路 002
update t_add a
set a.address_code = (select code from d_road b where a.address like '% ' || b.mc || '% ')
-----------------------
正解!