我试图在表格中找到“ - ”的数量,我正在使用此查询,但它无法正常工作
adapter.SelectCommand = new SqlCommand("select Count (*) from try
where DistrictName = '-' OR TownName = '-'
OR FarmerName = '-' OR Area = '-'"), con);*
此外,我如何将此查询转换为在每列中找到“ - ”的数字?
答案 0 :(得分:4)
关于你的第二个问题:
SELECT
SUM(CASE WHEN DistrictName = '-' THEN 1 ELSE 0 END) Districts,
SUM(CASE WHEN TownName = '-' THEN 1 ELSE 0 END) Towns,
SUM(CASE WHEN FarmerName = '-' THEN 1 ELSE 0 END) Farmers,
SUM(CASE WHEN Area = '-' THEN 1 ELSE 0 END) Areas
FROM try
对于第一个问题,请尝试此操作以获取所有列中的连字符总数:
SELECT
SUM((CASE WHEN DistrictName = '-' THEN 1 ELSE 0 END) +
(CASE WHEN TownName = '-' THEN 1 ELSE 0 END) +
(CASE WHEN FarmerName = '-' THEN 1 ELSE 0 END) +
(CASE WHEN Area = '-' THEN 1 ELSE 0 END)) AS Hyphens
FROM try