我想计算一列中的农民总数,但我希望我的查询避免在
中使用“ - ”列怎么能这样做。而不是Null值我的表有“ - ”。
adapter.SelectCommand = new SqlCommand("SELECT COUNT(FarmerName) AS
Total_Number_Of_Farmers FROM try", con);
答案 0 :(得分: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
如果a和b相等, NULLIF(a, b)
会返回NULL
。 COUNT(a)
仅计算NOT NULL
的值,因此您应该只计算非'-'
的值。
答案 1 :(得分:1)
SELECT COUNT(FarmerName) AS Total_Number_Of_Farmers
FROM try
WHERE FarmerName not like '%-%'