0

我有两张桌子,table1table2

table1 中的数据可能类似于:

Col1     col2
----     ----
NULL     NULL 
ABCD     NULL
NULL     DEFG
ABCD     DEFG

现在有另一个表包含相同的两列并包含任何这些数据的所有组合,例如(null, null)(null, defg)等。

我需要编写一个查询来获取匹配的行。

column1注意:匹配时应优先考虑。

4

1 回答 1

1

我认为你需要三个连接。您没有指定要执行的操作,因此以下查询仅指定匹配类型:

select table1.*,
       (case when fullmatch.col1 is not null and fullmatch.col2 is not null
             then 'FullMatch'
             when halfmatch1.col1 is not null
             then 'HalfMatch1'
             when halfmatch2.col2 is not null
             then 'HalfMatch2'
             else 'NoMatch'
        end) as MatchType
from table1 left outer join
     table2 fullmatch
     on table1.col1 = fullmatch.col1 and
        table1.col2 = fullmatch.col2 left outer join
     table2 halfmatch1
     on table1.col1 = halfmatch.col1 and
        halfmatch.col1 is null left outer join
     table2 halfmatch2
     on table1.col2 = halfmatch.col2 and
        halfmatch.col2 is null
于 2012-07-17T18:14:01.980 回答