当前位置: 代码迷 >> Oracle管理 >> 错误SQL语句却能执行的有关问题
  详细解决方案

错误SQL语句却能执行的有关问题

热度:96   发布时间:2016-04-24 05:51:14.0
求助:错误SQL语句却能执行的问题
SQL:select * from table_a where a_row_1 in (select a_row_1 from table_b where b_id = '123456');

单独执行括号中的select a_row_1 from table_b where b_id = '123456' 则会报错说table_b表中没有a_row_1字段
但整体执行这个SQL就可以得出结果,结果集和执行语句select * from table_a一样

求助:这是什么原因?SQL是怎么解释的?

------解决方案--------------------
不是被干掉了
而是没有起到过滤作用
(select a_row_1 from table_b where b_id = '123456')
首先 a_row_1 是从 table_a 表中查到的值 
比如 a_row_1 = 'Chinese'
那么在执行(select a_row_1 from table_b where b_id = '123456') 的时候
就变成了 (select 'Chinese' from table_b where b_id = '123456')
所以没有 起到过滤作用

------解决方案--------------------
sql语句应该这样写:
select * from table_a where a_row_1 in (select table_b.a_row_1 from table_b where b_id = '123456');
如果table_b中没有字段列a_row_1那一定会报错的。
  相关解决方案