我正在制作一个程序来检查数据库中15个信用卡读卡器的交易。该计划的目的是检查读者在预定日期范围内的销售情况,以确保我们没有机械问题。一般来说,如果读者没有在预定日期范围内记录销售,则应检查读者的操作。问题是,因为我“计算”交易数量,它会出现“0”而不是NULL,我只想收到一封电子邮件,如果没有任何交易......我该如何更改如果是这样,从“0”计数到NULL?我的查询如下:
SELECT COUNT(sTerminal) AS Terminal
FROM CC
WHERE (dtCreated BETWEEN @startdate AND @enddate) AND (sTerminal = 'Swiper 1')
答案 0 :(得分:6)
如果你想让计数为NULL,你可以这样做:
SELECT NULLIF(COUNT(sTerminal), 0) AS Terminal
FROM CC
WHERE (dtCreated BETWEEN @startdate AND @enddate) AND (sTerminal = 'Swiper 1')
如果您不想返回任何可能更有用的内容,可以执行HAVING
:
SELECT COUNT(sTerminal) AS Terminal
FROM CC
WHERE (dtCreated BETWEEN @startdate AND @enddate) AND (sTerminal = 'Swiper 1')
HAVING COUNT(sTerminal) > 0
答案 1 :(得分:3)
select CASE WHEN COUNT(sTerminal) = 0 THEN NULL ELSE COUNT(sTerminal) END AS Terminal
from ....
where ....
答案 2 :(得分:2)
为什么不在C#中处理这个逻辑:
var numberOfSales = GetNumberOfSales(); // Your sql will execute
if (numberOfSales > 0)
{
SendEmails();
}