有没有办法使用多个表

时间:2016-01-15 17:16:14

标签: sql sql-server

我正在尝试在名为#LIST的表中更新名为Specialty的列。我需要从3个不同的表中提取信息,因为1表中的专业是NULL。我更喜欢使用#Segment表中的专业,如果那是NULL,那么我想使用#SUBMARKET表中的专业,如果那是NULL,我想使用#MARKET表中的专业。

当我运行以下查询时,它不会更新任何行。我不想使用超过1个Update语句。我该怎么办?

UPDATE  #LIST   
SET Specialty = CASE WHEN   (l.Segment__c is not NULL)  THEN    s.Specialty
                    WHEN    (l.Segment__c is NULL)      THEN    b.Specialty
                    WHEN    (l.Submarket__c is NULL)    THEN    m.Specialty 
                    ELSE s.Specialty 
                END 
FROM #LIST l 
    join #MARKET m on m.Market = l.Market__c 
    join #SUBMARKET b on b.Submarket = l.Submarket__c
    join #Segment s on s.Segment = l.Segment__c

1 个答案:

答案 0 :(得分:1)

您需要使用Array ( [listen] => Array ( [445] => 445 [9091] => 9091 [3306] => 3306 [587] => 587 [11211] => 11211 [139] => 139 [80] => 80 [3312] => 3312 [51413] => 51413 [22] => 22 [631] => 631 [25] => 25 [443] => 443 [993] => 993 [143] => 143 ) [syn_recv] => Array ( [80] => 80 ) [time_wait] => Array ( [80] => 80 [51413] => 51413 ) [established] => Array ( [22] => 22 [9091] => 9091 [80] => 80 [3306] => 3306 ) [fin_wait2] => Array ( [80] => 80 ) ) 中的表别名。另外,我认为您需要UPDATE

left join

否则,UPDATE l SET Specialty = CASE WHEN (l.Segment__c is not NULL) THEN s.Specialty WHEN (l.Segment__c is NULL) THEN b.Specialty WHEN (l.Submarket__c is NULL) THEN m.Specialty ELSE s.Specialty END FROM #LIST l left join #MARKET m on m.Market = l.Market__c left join #SUBMARKET b on b.Submarket = l.Submarket__c left #Segment s on s.Segment = l.Segment__c; 值将过滤掉行,因为它们在连接中不匹配。