2016-01-21 76 views
0

您好我想從LEAD2複製到LEAD1。我有查詢選擇這些列,但我不知道如何更新set LEAD2=LEAD1。以下是選擇查詢:在內部連接的SQL更新和

SELECT 
     a.lead_id AS LEAD1 
     , b.lead_id AS LEAD2 
    FROM 
     dbo.spirulina AS a 
      INNER JOIN dbo.spirulina AS b 
       ON a.PHONEHOME = b.PHONEHOME 
    WHERE 
     (a.lead_id IS NOT NULL) 
     AND (b.lead_id IS NULL) 
    GROUP BY 
     a.lead_id 
     , b.lead_id 
    HAVING 
     (COUNT(a.PHONEHOME) > 1) 
+1

你能描述究竟是什麼你想在這裏做什麼?因爲可能有一種簡單的方法來實現您可能試圖通過此查詢來實現的內容。 – singhsac

+0

看起來像你想設置lead_id爲null當一個phonehome在多行? – dotjoe

+0

@dotjoe對不起我的錯。 lead2 = lead1 ... no lead1 = lead2 –

回答

0

您所要做的就是將表格設置爲UPDATE更改值的位置。從看起來你試圖用多個PhoneHome條目來處理lead_id。下面的查詢可能返回結果集:

SELECT 
    PHONEHOME 
    , COUNT(1)[Count] 
FROM 
    dbo.spirulina 
GROUP BY 
    PHONEHOME 
HAVING 
    COUNT(PHONEHOME) > 1 

如果這些是你要更新的記錄,只需更新表,並設置列(或多個)值

UPDATE 
    dbo.spirulina 
SET 
    lead_id = NULL 
FROM 
    dbo.spirulina 
GROUP BY 
    PHONEHOME 
HAVING 
    COUNT(PHONEHOME) > 1 
+0

是的,我已經通過例外來做到這一點:靠近組的錯誤語法 –

+0

RDMS正在使用什麼?它確實取決於某些句柄在更新語句中的不同聯接。請指教。請參閱上面的@ TT。的評論。 – Parfait