Sql Count查询(避免“ - ”条目)

时间:2013-11-15 09:55:50

标签: mysql sql

我想计算一列中的农民总数,但我希望我的查询避免在

中使用“ - ”

列怎么能这样做。而不是Null值我的表有“ - ”。

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

2 个答案:

答案 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)会返回NULLCOUNT(a)仅计算NOT NULL的值,因此您应该只计算非'-'的值。

答案 1 :(得分:1)

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