读书人

请问:数据库in条件中的查询语句解惑

发布时间: 2012-07-22 19:31:15 作者: rapoo

请教:数据库in条件中的查询语句解惑
一个sql查询语句中需用到in条件,因为条件中的id来自不同的表的集合,写了以下两个sql语句,第一种会报错,第二种可以,对此表示疑惑,代码如下:

1、select nvl(a.p_id,d.f_id)||','||nvl(b.c_id,d.f_id)||','||nvl(c.h_id,d.f_id)||','||d.f_id  from t_property a,     t_car  b,     t_human c,     t_claim dwhere a.f_id(+)=d.f_ID   and b.Cf_ID(+)=d.f_ID   and c.f_ID(+)=d.f_ID    and d.report_id='9050003202011001094';

result:651,229,651,651;
2、 select a.p_id from t_property a,t_claim dwhere a.f_id=d.f_ID and d.report_id='9050003202011001094' union  select b.c_id from t_car  b,t_claim d where b.f_id=d.f_ID and report_id='9050003202011001094' union  select c.h_id from t_human c,t_claim d where c.f_id=d.f_ID and d.report_id='9050003202011001094' union select f_id from t_claim d where d.report_id='9050003202011001094';

result:
651
229

两条sql语句的区别是前者返回一条结果,是符合条件的id的连接串(中间以逗号分隔),后者返回多条结果,如果前者是一个字符串作为in的条件也不应该报错的吧,最多是没有符合条件的结果;后者好理解,不知道是不是我自己的理解有错,还请有想法的朋友给个回复。

如果第一种写法是如我说的是一个id的连接串,in条件中一个数值,如果对这个sql语句返回值加工,类似于java中的split方法,当然要看oracle中有没有类似的函数,是不是也符合要求呢?^-^

读书人网 >其他数据库

热点推荐