SQLITE-如果一列为空,则另一列中的值为0

问题描述

我在表中有两列。表名是使用内部联接和分组依据构造的,我们将此表称为Joined。它有两列PresentScore。如果Present为空,那么我想为Score值赋0。

+------------+--------+-------------+------------+--------+
| Student_Id | Course | ExamDate    |  Present   | Score  |
+------------+--------+-------------+------------+--------+
|       1    | Math   | 04/05/2020  | Yes        | 45     |
|       2    | Math   | 04/05/2020  | NULL       | 90     |
|       2    | Math   | 04/05/2020  | NULL       | 50     |                     
+------------+--------+-------------+------------+--------+

我到目前为止是

SELECT DISTINCT StudentID,Course,ExamDate,Present,Score
CASE Present ISNULL
Score = 0
END
    FROM Joined

我需要与众不同,因为内部联接可以使我重复一些。我需要的是

+------------+--------+-------------+------------+--------+
| Student_Id | Course | ExamDate    |  Present   | Score  |
+------------+--------+-------------+------------+--------+
|       1    | Math   | 04/05/2020  | Yes        | 45     |
|       2    | Math   | 04/05/2020  | NULL       | 0     |
+------------+--------+-------------+------------+--------+

对我来说,这感觉非常非常错误,但是我无法弄清楚如何通过一个查询来做到这一点。我该怎么办?

解决方法

您可以尝试以下-

SELECT DISTINCT StudentID,Course,ExamDate,Present,Score,CASE when Present IS NULL
then 0 else score END as scoreval
FROM Joined
,

如果Present为null,那么我想为Score值赋0。

case表达式如下:

select 
    present,case when present is null 
        then 0 
        else score 
    end as score
from ...

present不是 null时,您不知道该怎么办-因此,这将返回原始的score

不清楚您为什么需要distinct。如果您要询问有关原始查询的问题,该查询似乎会产生(部分)重复项,则可以帮助解决该问题。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...