我有一张表Node,它的结构为StartPoint,EndPoint,tag。
数据为 StartPoint,EndPoint,tag
1 2 0
3 2 0
4 1 1
a b c
。。
我要将如果StartPoint为a并且tag为0的记录的StartPoint修改为d,并且查找,是否有StartPoint为d 和EndPoint为b相同的并且其tag为0的记录,如果有则同时将其tag修改为1。
我的想法是 update Node set StartPoint = d and tag =1 where (。。);
数据为 StartPoint,EndPoint,tag
1 2 0
3 2 0
4 1 1
a b c
。。
我要将如果StartPoint为a并且tag为0的记录的StartPoint修改为d,并且查找,是否有StartPoint为d 和EndPoint为b相同的并且其tag为0的记录,如果有则同时将其tag修改为1。
我的想法是 update Node set StartPoint = d and tag =1 where (。。);
update Node set tag=1 where (StartPoint=d and EndPoint=b and tag=0);
你的方法我试了一下,没有满足我的要求,可能我的表述不是很清楚吧
我的想法是 StartPoint,EndPoint,tag
1 2 0
3 2 0
4 1 1
1 2 1
。。
把StartPoint为3的修改为1,此时发现已经有StartPoint为1,EndPoint为2的这项,则应该将修改的这项的tag置为1。修改后的结果为
StartPoint,EndPoint,tag
1 2 0
1 2 1
4 1 1
1 2 1
。。
update Node set StartPoint=d,tag=decode(EndPoint,b,1,0) where tag=0 and (StartPoint=d)
这样好像可以了?
STARTPOINT ENDPOINT TAG
---------- ---------- ----------
1 2 0
1 2 1
3 4 0
3 5 0
3 2 0
3 2 1
1 4 0
4 5 0
我要将StartPoint为3的修改为1,修改后为
STARTPOINT ENDPOINT TAG
---------- ---------- ----------
1 2 0
1 2 1
1 4 0
1 5 0
1 2 0
1 2 1
1 4 0
4 5 0
发现已有 STARTPOINT 为1 ENDPOINT为2 和STARTPOINT 为1 ENDPOINT为4 则将其tag置为1。我的tag值是记录对应的是否曾经出现过,如果是第一次则为0,否则为1
二。用两条语句:
update Node set StartPoint = d where (StartPoint =a and tag=0);
update Node set tag=1 where (StartPoint=d and EndPoint=b and tag=0);
因为后面一个动作要依靠前面一个动作的结果。是串行。一个语句难搞定。
---------- ---------- ----------
1 2 0
1 2 1
3 4 0
3 5 0
3 2 0
3 2 1
1 4 0
4 5 0已选择8行。SQL> UPDATE test SET startpoint=1,
2 tag=decode((select count(*) from test a,
3 (select * from test where startpoint=3 or startpoint=1) b
4 where
5 ((a.startpoint=3 or a.startpoint=1) and a.endpoint=b.endpoint)),
6 0,0,1)
7 where startpoint=3;已更新4行。
SQL> select * from test;STARTPOINT ENDPOINT TAG
---------- ---------- ----------
1 2 0
1 2 1
1 4 1
1 5 1
1 2 1
1 2 1
1 4 0
4 5 0已选择8行。SQL>
3 (select * from test where startpoint=3 or startpoint=1) b
4 where
5 ((a.startpoint=3 or a.startpoint=1) and a.endpoint=b.endpoint),你用a,b来比较,但是你的a,b可能选择的是同一个表项,所以总是会出现count大于0的情况,不知道理解是否正确。应该避免a,b选择同一项的情况
---------- ---------- ----------
1 2 0
1 2 1
3 4 0
3 5 0
3 2 0
3 2 1
1 4 0
4 5 0已选择8行。SQL> UPDATE test b SET startpoint=1,
2 tag=decode((select count(*) from test a
3 where (a.startpoint=1 and a.endpoint=b.endpoint)),0,0,1)
4 where b.startpoint=3;已更新4行。SQL> select * from test;STARTPOINT ENDPOINT TAG
---------- ---------- ----------
1 2 0
1 2 1
1 4 1
1 5 0
1 2 1
1 2 1
1 4 0
4 5 0已选择8行。SQL>