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这条数据,但是写的这么屎,有没有什么好的方法实现?这个效率实在是底下
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这条数据,但是写的这么屎,有没有什么好的方法实现?这个效率实在是底下
这个是硬性规定? 还是说有什么规则的实际我看的话,你这个是取的 type 相同,children 取最小
你前台一次传入 3 组 type 和 children ?
那你现在查询的话,实际是拼接的 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';