日期:2014-05-17  浏览次数:21186 次

求助:错误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那一定会报错的。