我有一个简单的问题,我正在环顾四周,但不能让它适合我。我用当前日期做了一个查询,让我们说tomorow是星期二,但它还没有数据,因为它是未来。它还没有记录。
当我运行查询时,它只显示2个名为total | Date
的列
它做我说的。但是现在我希望它向我显示0
而不是注意,只有2个列。
我确实搜索过COALESCE,但由于某些原因它没有用,它也没有给我任何错误。
SELECT
SUM(totalExcl) AS total,
DATE_FORMAT(date_add, '%W') AS 'Date'
FROM
ex.ps_ox_quotation
WHERE
WEEK(date_add) = WEEK (UTC_TIMESTAMP())
AND saleType IN ('IEW' , 'A', 'Project')
AND DAYOFWEEK(date_add) % 6 > 4
GROUP BY Date
ORDER BY 'Date' DESC
我有什么(表)=
| |total | Date
我想要什么(表)=
| |total | Date
0 Tuesday
今天是星期三所以它是当前日期,这就是它显示的(顺便说一下,这是最新的)我只是展示它:
| |total | Date
500 Wednesday
答案 0 :(得分:1)
试试这个
SELECT
IF(SUM(totalExcl) is NULL,0,SUM(totalExcl)) AS total,
DAYNAME(date_add) AS 'Date'
FROM
ex.ps_ox_quotation
WHERE
WEEK(date_add) = WEEK (UTC_TIMESTAMP())
AND saleType IN ('IEW' , 'A', 'Project')
AND DAYOFWEEK(date_add) % 6 > 4
GROUP BY Date
ORDER BY 'Date' DESC
或者你可以这样做
SELECT
IF(SUM(totalExcl) is NULL,0,SUM(totalExcl)) AS total,
IF(DATE_FORMAT(date_add, '%W') is null,DAYNAME(now()),DATE_FORMAT(date_add, '%W')) AS 'Date'
FROM
ex.ps_ox_quotation
WHERE
WEEK(date_add) = WEEK (UTC_TIMESTAMP())
AND saleType IN ('IEW' , 'A', 'Project')
AND DAYOFWEEK(date_add) % 6 > 4
GROUP BY Date
ORDER BY 'Date' DESC
答案 1 :(得分:0)
尝试使用 IFNULL ...
SELECT
IFNULL(SUM(totalExcl),0) AS total,
DATE_FORMAT(date_add, '%W') AS 'Date'
FROM
ex.ps_ox_quotation
WHERE
WEEK(date_add) = WEEK (UTC_TIMESTAMP())
AND saleType IN ('IEW' , 'A', 'Project')
AND DAYOFWEEK(date_add) % 6 > 4
GROUP BY Date
ORDER BY 'Date' DESC