简体   繁体   English

多次自连接替换

[英]multiple self join replacement

Want some replacement for multiple self join on a table. 想要对表上的多个自连接进行一些替换。 what is the maximum limit of self join on a table in SQL SQL中的表自我连接的最大限制是多少

I have a table table_1 with columns as: 我有一个表table_1,其列为:

uid number(10),
a_name nvarchar(20),
aaId number (10),

where unique constraint is there on combination of uid and aaId. uid和aaId的组合存在唯一约束

data present in table is like. 表中存在的数据就像。 for each aaId there are around 90 Uid's. 每个aaId大约有90个Uid。

now my query is like 现在我的查询就像

select aaId from table_1 
  inner join table_1 t1 on t1.uid=9 and t1.a_name like 'a'  
  inner join table_1 t2 on t2.uid=8 and t2.a_name like 'ab'  
  inner join table_1 t3 on t3.uid=7 and t3.a_name like 'ac'

My problem is number of inner join has increased to 90 . 我的问题是内部联接的数量已增加到90。 as rows in table is around 2 lakh will this query work . 因为表中的行大约为20万,所以此查询有效。 Or if there is anything else I can do to replace this large number of self joins. 或者,如果还有其他方法可以替代大量的自我联接。

Please help. 请帮忙。

Thanks in advance. 提前致谢。

First of all, why don't you use the WHERE part with OR statement? 首先,为什么不将WHERE部分与OR语句一起使用?

select aaId from table_1 
WHERE (table_1.uid=9 and table_1.a_name like 'a') OR 
(table_1.uid=8 and table_1.a_name like 'ab') OR
(table_1.uid=7 and table_1.a_name like 'ac')

Please try this query which is used self join and required conditions 请尝试使用自连接和所需条件的此查询

select aaId from table_1 t1, table_1 t2 where (t1.uid=9 and t2.a_name like 'a') or (t1.uid=8 and t2.a_name like 'ab') 从table_1 t1,table_1 t2中选择aaId,其中(t1.uid = 9和t2.a_name如'a')或(t1.uid = 8和t2.a_name如'ab')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM