想要在桌面上替换多个自联接。 SQL中表的自连接的最大限制是什么
我有一个表table_1,列为:
uid number(10),
a_name nvarchar(20),
aaId number (10),
其中唯一约束位于 uid和aaId的组合上。
表中的数据就像。 对于每个aaId,大约有90个Uid。
现在我的查询就像
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'
我的问题是 内部联接的数量已增加到90。 因为表格中的行大约是2万卢比,这个查询会起作用。 或者如果还有什么我可以做的就是替换大量的自连接。
请帮忙。
提前致谢。
答案 0 :(得分:0)
首先,为什么不将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')
答案 1 :(得分:0)
请尝试使用自加入和必需条件的查询
从table_1 t1,table_1 t2中选择aaId 其中(t1.uid = 9和t2.a_name喜欢' a')或 (t1.uid = 8和t2.a_name喜欢' ab')