SQLITE - 如果一列为空,则另一列中的值为 0
SQLITE - If a column is null then the value in another column is 0
我在 table 中有两列。 table name 是用 inner join 和 group by 构造的,我们就叫这个 table Joined
。它有两列 Present
和 Score
。如果 Present
为 null 那么,我想将 0 分配给 Score
值。
+------------+--------+-------------+------------+--------+
| 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
我需要 distinct,因为内部联接可以给我一些重复。我需要的是
+------------+--------+-------------+------------+--------+
| 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
If Present is null then, I want to assign 0 to the Score value.
case
表达式如下:
select
present,
case when present is null
then 0
else score
end as score
from ...
当 present
不是 而不是 null
时,你不知道该怎么做 - 所以这个 returns 原来的 score
.
不清楚您为什么需要 distinct
。如果您要问一个关于原始查询的问题,它似乎产生(部分)重复项,也许可以帮助修复它。
我在 table 中有两列。 table name 是用 inner join 和 group by 构造的,我们就叫这个 table Joined
。它有两列 Present
和 Score
。如果 Present
为 null 那么,我想将 0 分配给 Score
值。
+------------+--------+-------------+------------+--------+
| 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
我需要 distinct,因为内部联接可以给我一些重复。我需要的是
+------------+--------+-------------+------------+--------+
| 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
If Present is null then, I want to assign 0 to the Score value.
case
表达式如下:
select
present,
case when present is null
then 0
else score
end as score
from ...
当 present
不是 而不是 null
时,你不知道该怎么做 - 所以这个 returns 原来的 score
.
不清楚您为什么需要 distinct
。如果您要问一个关于原始查询的问题,它似乎产生(部分)重复项,也许可以帮助修复它。