表 test id name type
-------------------
2 jj 1
3 gg 0
4 kk 1
2 uu 0
3 ww 1
-------------------要求:id相同的两条记录,把type=0的记录的name值更新为type=1的记录的name值。
不知道我描述的清楚不?
-------------------
2 jj 1
3 gg 0
4 kk 1
2 uu 0
3 ww 1
-------------------要求:id相同的两条记录,把type=0的记录的name值更新为type=1的记录的name值。
不知道我描述的清楚不?
where type=0 and exists (select 1 from test c where a.id=c.id and c.type=1)
我一个小时回来咯!update test a set a.name=(select b.name from test b where a.id=b.id and b.type=1 and rownum=1)
where type=0 and exists (select 1 from test c where a.id=c.id and c.type=1);
update test_tt a set a.name=(select b.name from test_tt b where a.id=b.id and b.type=1)
where exists(select 1 from test_tt c where a.id=c.id and c.type=1) and a.type=0--不是的有多的 则加个伪列update test_tt a set a.name=(select b.name from test_tt b where a.id=b.id and b.type=1 and rownum=1)
where exists(select 1 from test_tt c where a.id=c.id and c.type=1) and a.type=0
--试试这个的更新时间报下
update (select /*+ BYPASS_UJVC */ a.name name1,b.name name2
from test_t1 a inner join test_t1 b on a.id=b.id
where a.type=1 and b.type=0)
set name2=name1
update test t1 set(t1.name)=(select t2.name from test t2 where t1.id=t2.id and t2.type=0) where t1.type=1
我试了这样子更新 可以 但是貌似只是Oracle适用
--befor--
/*id name type
2 jj 1
4 kk 1
2 uu 0
3 ww 1
3 gg 0*/
--sql
update test t set name=(select name from test where t.id=id and type=1)--after--
/* id name type
2 jj 1
4 kk 1
2 jj 0
3 ww 1
3 ww 0*/
你把你 4 kk 1 这条记录中type=1改成type=0试试你的结果呢!