[英]multiple self join replacement
想要對表上的多個自連接進行一些替換。 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。 因為表中的行大約為20萬,所以此查詢有效。 或者,如果還有其他方法可以替代大量的自我聯接。
請幫忙。
提前致謝。
首先,為什么不將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')
請嘗試使用自連接和所需條件的此查詢
從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.