本来要写个update语句
update table_a A
set A.flag=1
where A.id in (select B.id from table_b B)
结果误写成
update table_a A
set A.flag=1
where A.id in (select A.id from table_b B)
为啥错误的写法,oralce编译不报错呢?
而且 只要B表中有数据,A就全表更新了
B表没数据,A表就update了0条记录,
谁能解释下?是oracle的bug么?
update table_a A
set A.flag=1
where A.id in (select B.id from table_b B)
结果误写成
update table_a A
set A.flag=1
where A.id in (select A.id from table_b B)
为啥错误的写法,oralce编译不报错呢?
而且 只要B表中有数据,A就全表更新了
B表没数据,A表就update了0条记录,
谁能解释下?是oracle的bug么?
你说的错误的写法只是和你的本意不一致,语法上没错
提示:ora-00904的错误!
select * from test a
where name in(
select a.name from test b)这个就不提示,直接显示
select * from test a
B中没有数据,就更新0行;
set A.flag=1
where A.id in (select A.id from table_b B)
可以这样理解吧:。
如果B有数据,那么子查询返回A的所有id,A表全部更新;
如果B没有数据,子查询返回空集合,A表0行更新。
你这个不是语法错误,是逻辑错误,oracle编译的时候,检查语法而已,他不知道你需要的逻辑。
恩,没有错,老大说的对,是逻辑错误,oracle编译的时候只检查语法不检查逻辑的。
where A.id in (select '' from table_b B)效果是一样的!
若b表有记录,第二句会将a表的每条记录都作修改
那个条件没有意义
若b表没有记录,则a表修改的记录数为0
---------- --------------------
1 wh
2 wp
3 wrSQL> select * from test3; ID NAME
---------- --------------------
3 www
1 rrrr
6 hhh
SQL> update test1 a
2 set a.name='xxx'
3 where a.id in (select a.id from test3 b)
4 ;3 rows updated.
SQL> update test1 a
2 set a.name='xxx'
3 where a.id in (select a.id from dual)
4 ;3 rows updated.SQL> rollback;Rollback complete.SQL> update test1 a
2 set a.name='xxx'
3 where a.id in (select null from dual)
4 ;0 rows updated.
你的说法好像不完全正确哦。
select a.id from dual和select null from dual有什么区别吗
为什么执行结果会相反?BTW: 这算是一个BUG吗??
set A.flag=1
where A.id in (select A.id from table_b B) 语法上没有错。并且结果应该是更新你A表中所有记录。
select a.id from dual和select null from dual当然有区别。
子查询是a.id IN (.....), 你select null 肯定最终结果是False
2 set a.name='xxx'
3 where a.id in (select a.id from dual)
4 ;3 rows updated.SQL> rollback;Rollback complete.SQL> update test1 a
2 set a.name='xxx'
3 where a.id in (select null from dual)
4 ;0 rows updated.
不太明白,为什么select a.id from dual成功而select null from dual却不成功
dual中明明也没有a.id这个字段呀,按说IN操作符后面不是应该是个值列表吗
只有a.id符合值列表中的某个值的时候where条件才会为真,才会进行修改
取的是子查询外面的test1.id。。
本来要写个update语句
update table_a A
set A.flag=1
where A.id in (select B.id from table_b B)
你这个写法有表a
select a.id from dual可以查询出来A表的ID列的值,where条件成立,所以它会更新.而select null from dual查不到A表中ID列的值,所以where条件不成立,当然不会更新.