2016-05-26 38 views
0

我有我的表這種線:SQL連接字符串沒有重複的行

id email1    email2    email3    all 
1 [email protected] [email protected]  [email protected] NULL 
2 [email protected] NULL     [email protected] NULL 
3 [email protected] NULL     [email protected] NULL 
4 [email protected] [email protected]   NULL    NULL 
5 [email protected] NULL     [email protected] NULL 
6 [email protected] [email protected]  [email protected] NULL 

有了這樣的要求:

UPDATE mytable SET all=concat_ws(';', email1, email2, email3); 

我:

id email1    email2    email3    all 
1 [email protected] [email protected]  [email protected] [email protected];[email protected];[email protected] 
2 [email protected] NULL     [email protected] [email protected];[email protected] 
3 [email protected] NULL     [email protected] [email protected];[email protected] 
4 [email protected] [email protected]   NULL    [email protected];[email protected] 
5 [email protected] NULL     [email protected] [email protected];[email protected] 
6 [email protected] [email protected]  [email protected] [email protected];[email protected];[email protected] 

但我也需要刪除重複的電子郵件進行編輯:

id email1    email2    email3    all 
1 [email protected] [email protected]  [email protected] [email protected];[email protected] 
2 [email protected] NULL     [email protected] [email protected] 
3 [email protected] NULL     [email protected] [email protected];[email protected] 
4 [email protected] [email protected]   NULL    [email protected];[email protected] 
5 [email protected] NULL     [email protected] [email protected] 
6 [email protected] [email protected]  [email protected] [email protected];[email protected];[email protected] 

感謝您的幫助!

回答

2

這可能不是理想的解決方案,但你可以嘗試case ... end條款,以確定重複:

UPDATE mytable SET all=concat_ws(
    ';' 
    , email1 
    , case when email2 in (email1) then null else email2 end 
    , case when email3 in (email1,email2) then null else email3 end); 
+0

是啊,我不知道這是否是最好的解決辦法,但它的工作原理,謝謝 – Macbernie