2013-11-15 48 views
1

我想算在列農民的總數,但我想我的查詢,以避免「 - 」在的Sql數查詢(避免「 - 」的條目)

的專欄中,我該怎麼辦that.Instead中我的表的空值有「 - 」。

adapter.SelectCommand = new SqlCommand("SELECT COUNT(FarmerName) AS 
Total_Number_Of_Farmers FROM try", con); 

回答

3

我可能會誤解這個問題,但這不起作用嗎?

SELECT COUNT(FarmerName) AS Total_Number_Of_Farmers 
FROM try 
WHERE FarmerName <> '-' 

adapter.SelectCommand = new SqlCommand("SELECT COUNT(FarmerName) AS Total_Number_Of_Farmers FROM try WHERE FarmerName <> '-'", con); 

如果您需要一次(如在意見中的要求)做了多列,下面應該工作:

SELECT COUNT(DISTINCT(NULLIF(DistrictName,'-'))) AS Unique_DistrictNames 
    ,COUNT(DISTINCT(NULLIF(TownName,'-'))) AS Unique_TownNames 
FROM try 

NULLIF(a, b)返回NULL如果和b是一樣的。 COUNT(a)只計算值爲NOT NULL的值,因此這種方式您應該只計算不是'-'的值。

+0

是它的工作... – user1056466

+0

我向你保證,另一種是慢,如果一個農民有一個雙姓就會失敗.. – OGHaza

+0

確定的,如果我要算「 - 」在所有的列我將如何做到這一點? adapter.SelectCommand = new SqlCommand(「select count(*)from try where DistrictName =' - 'OR TownName =' - '」,con)但這不能正常工作 – user1056466

1
SELECT COUNT(FarmerName) AS Total_Number_Of_Farmers 
FROM try 
WHERE FarmerName not like '%-%'