2016-05-21 127 views
0
SELECT IF(COUNT(cm.CUSTOMER_ID)>0,COUNT(cm.CUSTOMER_ID),0) COUNT 
FROM customer_master cm 
JOIN customer_issue_details ci USING (customer_id) 
WHERE ci.ACTUAL_DATE_RETURN < ci.RETURN_DATE 
    AND CUSTOMER_NAME LIKE 'r%' 
GROUP BY cm.`CUSTOMER_ID`; 

當沒有找到這樣的記錄時,它不顯示0,但只顯示別名計數。如何得到0下count.I使用ifnull,ifisnull也沒用。 請任何一個可以告訴這是爲什麼不工作,如何獲取0在mysql中它沒有顯示計數值0

回答

0

未經測試,請嘗試使用由cm.CUSTOMER_ID而是由ci.CUSTOMER_ID不分組。另外,我認爲你需要一個LEFT JOIN

SELECT COUNT(ci.CUSTOMER_ID) 
FROM customer_master cm 
LEFT JOIN customer_issue_details ci USING (customer_id) 
WHERE ci.ACTUAL_DATE_RETURN < ci.RETURN_DATE 
    AND CUSTOMER_NAME LIKE 'r%' 
GROUP BY ci.CUSTOMER_ID; 

下面是SQL小提琴簡化無ACTUAL_DATE_RETURN並沒有RETURN_DATEhttp://sqlfiddle.com/#!9/73cc64/2/0

更新1

在烏爾撥弄如果u改變 '%R' 到 '%一個' 這不是甚至顯示輸出表,但它應該已經給出了0表下的名稱

這就是SQL的工作原理。由於沒有名稱與該表達式匹配的客戶,因此不會得到任何結果行。您需要檢查應用程序中設置的空結果。

+0

@Stone:請參閱SQL小提琴。 – wilx

+0

@wix ...謝謝兄弟...在我的sqlYog它的輸出爲任何計數> 0但不是計數= 0 .....在你的小提琴如果你改變'%r'爲'%a'它是甚至沒有顯示輸出表,但它應該已經給出了0下的表名 – Stone

+0

@Stone:我不明白你在說什麼。請詳細說明。 – wilx

2

您需要使用LEFT JOIN。並且任何ci列的每個條件都必須在ON子句中(否則,LEFT JOIN將轉換爲INNER JOIN)。而且你必須計數ci.CUSTOMER_ID而不是cm.CUSTOMER_ID

SELECT cm.CUSTOMER_ID, COUNT(ci.CUSTOMER_ID) COUNT 
FROM customer_master cm 
LEFT JOIN customer_issue_details ci 
    ON ci.customer_id = cm.CUSTOMER_ID 
    AND ci.ACTUAL_DATE_RETURN < ci.RETURN_DATE 
WHERE CUSTOMER_NAME LIKE 'r%' 
GROUP BY cm.`CUSTOMER_ID`; 

內連接(JOIN是INNER JOIN的別名)將從cm表過濾掉任何行,如果沒有從ci錶行已發現匹配聯接條件(如果你使用不要緊USINGON)。 LEFT JOIN將從cm表中返回至少一行,但如果ci表中沒有找到匹配JOIN條件的行,則ci表中的所有列都將爲NULL。

實施例:

cm: 
| customer_id | 
|-------------| 
|   1 | 
|   2 | 
|   3 | 

ci: 

| customer_id | 
|-------------| 
|   1 | 
|   1 | 
|   2 | 

INNER JOIN:

SELECT cm.customer_id as `cm.customer_id`, ci.customer_id as `ci.customer_id` 
FROM cm 
JOIN ci 
    ON ci.customer_id = cm.customer_id; 

| customer_id | customer_id | 
|-------------|-------------| 
|   1 |   1 | 
|   1 |   1 | 
|   2 |   2 | 

LEFT JOIN:

SELECT cm.customer_id, ci.customer_id 
FROM cm 
LEFT JOIN ci 
    ON ci.customer_id = cm.customer_id; 

| customer_id | customer_id | 
|-------------|-------------| 
|   1 |   1 | 
|   1 |   1 | 
|   2 |   2 | 
|   3 |  (null) | 

fiddle

隨着GROUP BY cm.customer_idCOUNT(ci.customer_id)您可以計算每個找到的行數cm.customer_id

SELECT cm.customer_id, COUNT(ci.customer_id) 
FROM cm 
LEFT JOIN ci 
    ON ci.customer_id = cm.customer_id 
GROUP BY cm.customer_id 

| customer_id | COUNT(ci.customer_id) | 
|-------------|-----------------------| 
|   1 |      2 | 
|   2 |      1 | 
|   3 |      0 | 

fiddle

它返回0cm.customer_id = 3因爲COUNT只計算不爲空值。

如果您使用COUNT(cm.customer_id)而不是cm.customer_id = 3,您將獲得1,因爲它不是NULL。現在,如果你有一列的任何條件ci表(如ci.customer_id < 2),你把它的WHERE子句中fiddle

,不匹配條件的所有行都會被過濾掉。

SELECT cm.customer_id, ci.customer_id 
FROM cm 
LEFT JOIN ci 
    ON ci.customer_id = cm.customer_id 
WHERE ci.customer_id < 2 

| customer_id | customer_id | 
|-------------|-------------| 
|   1 |   1 | 
|   1 |   1 | 

但移動該條件爲LEFT JOIN ON子句你保持每個cm.customer_id至少一排,因爲這是多麼LEFT JOIN的作品。

SELECT cm.customer_id, ci.customer_id 
FROM cm 
LEFT JOIN ci 
    ON ci.customer_id = cm.customer_id 
    AND ci.customer_id < 2 

| customer_id | customer_id | 
|-------------|-------------| 
|   1 |   1 | 
|   1 |   1 | 
|   2 |  (null) | 
|   3 |  (null) | 

現在GROUP BYCOUNT

SELECT cm.customer_id, COUNT(ci.customer_id) 
FROM cm 
LEFT JOIN ci 
    ON ci.customer_id = cm.customer_id 
    AND ci.customer_id < 2 
GROUP BY cm.customer_id; 

| customer_id | COUNT(ci.customer_id) | 
|-------------|-----------------------| 
|   1 |      2 | 
|   2 |      0 | 
|   3 |      0 | 

sqlfiddle

+0

Thanx,它的工作.....我正在寫USING子句,而不是ON子句that'why o/p沒有來... .......請告訴你爲什麼剛剛在ON子句後寫入(AND ci.ACTUAL_DATE_RETURN Stone

+0

@Stone,我希望這個更新能夠回答你所有的問題:-) –

+0

thanx幫助 – Stone