2014-10-27 28 views
0

我們有2代表與英文單詞:words_1和words_2用字段(字爲VARCHAR,REF爲INT),其中字 - 這是一個英文單詞,樓盤 - 對另一個(第三)表引用(這並不重要)。大內連接

在每個表中所有的字都是唯一的。第一個表格包含一些不在第二個表格中的單詞(相反,第二個表格包含一些獨特的單詞)。在兩個表

但大多數的話是一樣的。

需要去:結果表與所有不同的詞和參考的。

爲相同的表初始條件

  1. 價的可以是不同的(字典從不同的地方加載)。
  2. 字數計算每表300 000,所以內部聯接是不是方便易

例子

words_1 
________ 
Health-1 
Car-3 
Speed-5 

words_2 
_________ 
Health-2 
Buty-6 
Fast-8 
Speed-9 

Result table 
_____________ 
Health-1 
Car-3 
Speed-5 
Buty-6 
Fast-8 
+0

您的結果不顯示「所有不同的詞和ref」。它顯示了所有區別詞與一個REF(默認爲words_1.ref或至少(words_1.ref,words_2.ref)在歧義的情況下 - 這是不清楚哪個)。 – 2014-10-27 13:54:38

回答

0

嘗試使用full outer join

select coalesce(w1.word, w2.word) as word, coalesce(w1.ref, w2.ref) as ref 
from words_1 w1 full outer join 
    words_2 w2 
    on w1.word = w2.word; 

唯一的一次,這將不工作是如果ref可以在任一表中是NULL。在這種情況下,改變on到:

on w1.word = w2.word and w1.ref is not null and w2.ref is not null 

如果你想提高性能,只需要創建在表上的索引:

create index idx_words1_word_ref on words_1(word, ref); 
create index idx_words2_word_ref on words_2(word, ref); 

一個join是相當可行的,即使沒有索引,SQL服務器應該足夠聰明以提供合理的實現。

1
select word,min(ref) 
from (
    select word,ref 
    from words_1 
    union all 
    select word,ref 
    from words_2 
    ) t 
group by word