2011-09-28 43 views
25

我有以下查詢:如何有SQL INNER JOIN接受空結果

SELECT TOP 25 CLIENT_ID_MD5, COUNT(CLIENT_ID_MD5) TOTAL 
FROM dbo.amazonlogs 
GROUP BY CLIENT_ID_MD5 
ORDER BY COUNT(*) DESC; 

將返回:

283fe255cbc25c804eb0c05f84ee5d52 864458 
879100cf8aa8b993a8c53f0137a3a176 126122 
06c181de7f35ee039fec84579e82883d 88719 
69ffb6c6fd5f52de0d5535ce56286671 68863 
703441aa63c0ac1f39fe9e4a4cc8239a 47434 
3fd023e7b2047e78c6742e2fc5b66fce 45350 
a8b72ca65ba2440e8e4028a832ec2160 39524 
... 

我想用返回的MD5,以獲取相應的客戶端名稱(FIRM)從該查詢,那麼行可能看起來像:

879100cf8aa8b993a8c53f0137a3a176 126122 Burger King 

所以我做了這個查詢:

SELECT a.CLIENT_ID_MD5, COUNT(a.CLIENT_ID_MD5) TOTAL, c.FIRM 
FROM dbo.amazonlogs a 
    INNER JOIN dbo.customers c 
    ON c.CLIENT_ID_MD5 = a.CLIENT_ID_MD5 
GROUP BY a.CLIENT_ID_MD5, c.FIRM 
ORDER BY COUNT(*) DESC; 

這將返回類似:

879100cf8aa8b993a8c53f0137a3a176 126122 Burger King 
06c181de7f35ee039fec84579e82883d 88719  McDonalds 
703441aa63c0ac1f39fe9e4a4cc8239a 47434  Wendy's 
3fd023e7b2047e78c6742e2fc5b66fce 45350  Tim Horton's 

其中一期工程,但我需要的,如果有一個給定的MD5沒有相應的公司來返回c.FIRM空值。例如:

879100cf8aa8b993a8c53f0137a3a176 126122 Burger King 
06c181de7f35ee039fec84579e82883d 88719  McDonalds 
69ffb6c6fd5f52de0d5535ce56286671 68863 
703441aa63c0ac1f39fe9e4a4cc8239a 47434  Wendy's 
3fd023e7b2047e78c6742e2fc5b66fce 45350  Tim Horton's 

我應該如何修改查詢仍然返回一行,即使沒有相應的c.FIRM?

+10

它是黑色的。你可能會被吃掉。 – SQLMason

回答

81

LEFT JOIN

+6

即使在您加入的其他表格中沒有匹配時,LEFT JOIN也會返回行。 INNER JOIN不會,它只會返回行,如果在其他表匹配 – evilone

+0

所以*這是*的差異... – Jackson

2

內部聯接排除空值替換INNER JOIN;你想要一個LEFT OUTER連接。

1
SELECT a.CLIENT_ID_MD5, COUNT(a.CLIENT_ID_MD5) TOTAL, IsNull(c.FIRM, 'Unknown') as Firm 
FROM dbo.amazonlogs a 
    LEFT JOIN dbo.customers c ON c.CLIENT_ID_MD5 = a.CLIENT_ID_MD5 
GROUP BY a.CLIENT_ID_MD5, c.FIRM ORDER BY COUNT(*) DESC; 

當客戶表中的記錄不存在時,這會給你一個「未知」值。你顯然可以放棄那部分,只要返回c.FIRM,如果你想要實際的空值。

2

而不是做一個內部聯接,你應該做一個LEFT OUTER JOIN:

SELECT 
    a.CLIENT_ID_MD5, 
    COUNT(a.CLIENT_ID_MD5) TOTAL, 
    ISNULL(c.FIRM,'') 
FROM 
    dbo.amazonlogs a LEFT OUTER JOIN 
    dbo.customers c ON c.CLIENT_ID_MD5 = a.CLIENT_ID_MD5 
GROUP BY 
    a.CLIENT_ID_MD5, 
    c.FIRM 
ORDER BY COUNT(0) DESC 

http://www.w3schools.com/sql/sql_join.asp

+0

您在查詢中有一個錯字 - LEFT OERER JOIN – evilone

+0

Cheers evilone .. –

9

使用LEFT JOIN代替INNER JOIN

1

改變你的INNER JOIN到外連接.. 。

SELECT a.CLIENT_ID_MD5, COUNT(a.CLIENT_ID_MD5) TOTAL, c.FIRM 
FROM dbo.amazonlogs a 
LEFT OUTER JOIN dbo.customers c 
    ON c.CLIENT_ID_MD5 = a.CLIENT_ID_MD5 
GROUP BY a.CLIENT_ID_MD5, c.FIRM 
ORDER BY COUNT(*) DESC; 
1
WITH amazonlogs_Tallies 
    AS 
    (
     SELECT a.CLIENT_ID_MD5, COUNT(a.CLIENT_ID_MD5) TOTAL 
     FROM dbo.amazonlogs a 
     GROUP 
      BY a.CLIENT_ID_MD5 
    ), 
    amazonlogs_Tallies_Firms 
    AS 
    (
     SELECT a.CLIENT_ID_MD5, a.TOTAL, c.FIRM 
     FROM amazonlogs_Tallies a 
      INNER JOIN dbo.customers c 
       ON c.CLIENT_ID_MD5 = a.CLIENT_ID_MD5 
    ) 
SELECT CLIENT_ID_MD5, TOTAL, FIRM 
    FROM amazonlogs_Tallies_Firms 
UNION 
SELECT CLIENT_ID_MD5, TOTAL, '{{NOT_KNOWN}}' 
    FROM amazonlogs_Tallies 
EXCEPT 
SELECT CLIENT_ID_MD5, TOTAL, '{{NOT_KNOWN}}' 
    FROM amazonlogs_Tallies_Firms;