2012-09-03 39 views
0

想要替換一個表上的多個自加入。自我的最大限制在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

你爲什麼使用內部連接?它服務的目的是什麼,你試圖提取哪些數據? – Ankur

+0

是的,請解釋你想做什麼。這從代碼中看不出來。我在猜測你只是想選擇表中有一組完整記錄的aaIds,但很難說清楚。 –

+0

一個例子(數據樣本及其預期結果)將是最受歡迎的。 –

回答

0

首先,爲什麼你不使用帶有OR語句的WHERE部分?

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') 
0

請嘗試此查詢其用於自連接和所需的條件

從TABLE_1 T1選擇AAID,TABLE_1 T2 其中(t1.uid = 9和t2.a_name像 'A')或 (t1.uid = 8和t2.a_name like'ab')