table1
id   type    parent   children
1      0       2233     67
2      0       2233     68
3      0       2234     69
4      0       2234     70
5      1       2233     15
6      1       2233     16
7      1       2234     15
8      1       2234     16
9      2       2233     55
10     2       2234     56table2
id    name  description
2233   aa      aaaaaaa
2234   bb      bbbbbbb
2235   cc      ccccccc其中table1中的parent和table2中的id是对应的
现在要求跟据表table1中type=0,children=67    以及   type=1,children=15    以及   type=2,children=55三组数据查找到table2中的2233这条数据
我现在的写法是:select * from table2 where id in(
select a.a
from (
select t.parent a
from table1 t
where t.type = 0 and t.children = 67
) a, (
select t.parent a
from table1 t
where t.type = 1 and t.children = 15
) b, (
select t.parent a
from table1 t
where t.type = 2 and t.children = 55
)
where a.a = b.a and a.a = c.a
group by a.a
)最终得到
id   name     desctipion
2233  aa        aaaaa这条数据,但是写的这么屎,有没有什么好的方法实现?这个效率实在是底下

解决方案 »

  1.   

    type=0,children=67, type=1,children=15, type=2,children=55
    这个是硬性规定? 还是说有什么规则的实际我看的话,你这个是取的 type 相同,children 取最小
      

  2.   


    你前台一次传入 3 组 type 和 children ?
      

  3.   

    传入的是数组吗?
    那你现在查询的话,实际是拼接的 sql 了?
      

  4.   

    传入的数据类型什么样都可以,现在是在后台拼的SQL,因为我想的到写法,实在是复杂,不知道怎么写才能简单高效一些
      

  5.   

    既然是拼接的 sql,你可以把传入的参数拼一个嵌套查询的 sql
    例如
    sql := 'select 0 as type, 67 as children from dual
             union all
            select 1 as type, 15 as children from dual
             union all
            select 2 as type, 55 as children from dual'sql := 'select distinct parent from table1 a, ' || chr(10)
        || '(' || sql || ') b' || chr(10)
        || ' where a.type = b.type and a.children = b.children ';sql := 'select * from table2 a , ' || chr(10)
        || '(' || sql || ') b' || chr(10)
        || ' where a.name = b.parent';