嵌套查询中的 COALESCE
COALESCE in Nested Query
尝试阻止此查询在结果(在本例中为 lessons.classDate)不存在时返回 NULL。我确定 COALESCE 应该被环绕,但我无法正确理解语法。
SELECT FLOOR(COUNT(S0001)* 100 /
(SELECT COUNT(*) FROM stat_P01627
INNER JOIN lessons ON stat_P01627.lesson = lessons.id
INNER JOIN modules ON lessons.module = modules.id
WHERE lessons.module = 'MHG405294' AND lessons.classDate >= DATE_ADD(CURDATE(),INTERVAL -12 WEEK))
) AS stat12wkMod1
FROM attendance_P01627
INNER JOIN lessons ON stat_P01627.lesson = lessons.id
INNER JOIN modules ON lessons.module = modules.id
WHERE lessons.module = 'MHG405294' AND lessons.classDate >= DATE_ADD(CURDATE(),INTERVAL -12 WEEK);
我在努力;
SELECT FLOOR(COUNT(S0001)* 100 /
COALESCE(SELECT COUNT(*) FROM stat_P01627
INNER JOIN lessons ON stat_P01627.lesson = lessons.id
INNER JOIN modules ON lessons.module = modules.id
WHERE lessons.module = 'MHG405294' AND lessons.classDate >= DATE_ADD(CURDATE(),INTERVAL -12 WEEK)),0)
) AS stat12wkMod1
FROM stat_P01627
INNER JOIN lessons ON stat_P01627.lesson = lessons.id
INNER JOIN modules ON lessons.module = modules.id
WHERE lessons.module = 'MHG405294' AND lessons.classDate >= DATE_ADD(CURDATE(),INTERVAL -12 WEEK);
[编辑]
工作版本,供感兴趣的人使用。
SELECT IFNULL(FLOOR(COUNT(S0001)* 100 /
IFNULL((SELECT COUNT(*) FROM stat_P01627
INNER JOIN lessons ON stat_P01627.lesson = lessons.id
INNER JOIN modules ON lessons.module = modules.id
WHERE lessons.module = 'MHG405294' AND lessons.classDate >= DATE_ADD(GETDATE(),INTERVAL -12 WEEK)),1)) ,1)
AS stat12wkMod1
FROM stat_P01627
INNER JOIN lessons ON stat_P01627.lesson = lessons.id
INNER JOIN modules ON lessons.module = modules.id
WHERE lessons.module = 'MHG405294' AND lessons.classDate >= DATE_ADD(GETDATE(),INTERVAL -12 WEEK);
对 SQL-SERVER 使用 Isnull,对 MySQL
使用 IFNULL
SELECT FLOOR(COUNT(S0001)* 100 /
IFNULL((SELECT COUNT(*) FROM stat_P01627
INNER JOIN lessons ON stat_P01627.lesson = lessons.id
INNER JOIN modules ON lessons.module = modules.id
WHERE lessons.module = 'MHG405294' AND lessons.classDate >= DATE_ADD(CURDATE(),INTERVAL -12 WEEK)),1)
) AS stat12wkMod1
FROM stat_P01627
INNER JOIN lessons ON stat_P01627.lesson = lessons.id
INNER JOIN modules ON lessons.module = modules.id
WHERE lessons.module = 'MHG405294' AND lessons.classDate >= DATE_ADD(CURDATE(),INTERVAL -12 WEEK);
https://docs.microsoft.com/en-us/sql/t-sql/language-elements/coalesce-transact-sql
The ISNULL OR IFNULL function and the COALESCE expression have a similar purpose
but can behave differently.
Because ISNULL is a function, it is
evaluated only once.
As described above, the input values for the
COALESCE expression can be evaluated multiple times. Data type
determination of the resulting expression is different. ISNULL uses
the data type of the first parameter, COALESCE follows the CASE
expression rules and returns the data type of value with the highest
precedence.
The NULLability of the result expression is different for
ISNULL and COALESCE. The ISNULL return value is always considered NOT
NULLable (assuming the return value is a non-nullable one) whereas
COALESCE with non-null parameters is considered to be NULL. So the
expressions ISNULL(NULL, 1) and COALESCE(NULL, 1) although equivalent
have different nullability values. This makes a difference if you are
using these expressions in computed columns, creating key constraints
or making the return value of a scalar UDF deterministic so that it
can be indexed as shown in the following example.
尝试阻止此查询在结果(在本例中为 lessons.classDate)不存在时返回 NULL。我确定 COALESCE 应该被环绕,但我无法正确理解语法。
SELECT FLOOR(COUNT(S0001)* 100 /
(SELECT COUNT(*) FROM stat_P01627
INNER JOIN lessons ON stat_P01627.lesson = lessons.id
INNER JOIN modules ON lessons.module = modules.id
WHERE lessons.module = 'MHG405294' AND lessons.classDate >= DATE_ADD(CURDATE(),INTERVAL -12 WEEK))
) AS stat12wkMod1
FROM attendance_P01627
INNER JOIN lessons ON stat_P01627.lesson = lessons.id
INNER JOIN modules ON lessons.module = modules.id
WHERE lessons.module = 'MHG405294' AND lessons.classDate >= DATE_ADD(CURDATE(),INTERVAL -12 WEEK);
我在努力;
SELECT FLOOR(COUNT(S0001)* 100 /
COALESCE(SELECT COUNT(*) FROM stat_P01627
INNER JOIN lessons ON stat_P01627.lesson = lessons.id
INNER JOIN modules ON lessons.module = modules.id
WHERE lessons.module = 'MHG405294' AND lessons.classDate >= DATE_ADD(CURDATE(),INTERVAL -12 WEEK)),0)
) AS stat12wkMod1
FROM stat_P01627
INNER JOIN lessons ON stat_P01627.lesson = lessons.id
INNER JOIN modules ON lessons.module = modules.id
WHERE lessons.module = 'MHG405294' AND lessons.classDate >= DATE_ADD(CURDATE(),INTERVAL -12 WEEK);
[编辑] 工作版本,供感兴趣的人使用。
SELECT IFNULL(FLOOR(COUNT(S0001)* 100 /
IFNULL((SELECT COUNT(*) FROM stat_P01627
INNER JOIN lessons ON stat_P01627.lesson = lessons.id
INNER JOIN modules ON lessons.module = modules.id
WHERE lessons.module = 'MHG405294' AND lessons.classDate >= DATE_ADD(GETDATE(),INTERVAL -12 WEEK)),1)) ,1)
AS stat12wkMod1
FROM stat_P01627
INNER JOIN lessons ON stat_P01627.lesson = lessons.id
INNER JOIN modules ON lessons.module = modules.id
WHERE lessons.module = 'MHG405294' AND lessons.classDate >= DATE_ADD(GETDATE(),INTERVAL -12 WEEK);
对 SQL-SERVER 使用 Isnull,对 MySQL
使用 IFNULLSELECT FLOOR(COUNT(S0001)* 100 /
IFNULL((SELECT COUNT(*) FROM stat_P01627
INNER JOIN lessons ON stat_P01627.lesson = lessons.id
INNER JOIN modules ON lessons.module = modules.id
WHERE lessons.module = 'MHG405294' AND lessons.classDate >= DATE_ADD(CURDATE(),INTERVAL -12 WEEK)),1)
) AS stat12wkMod1
FROM stat_P01627
INNER JOIN lessons ON stat_P01627.lesson = lessons.id
INNER JOIN modules ON lessons.module = modules.id
WHERE lessons.module = 'MHG405294' AND lessons.classDate >= DATE_ADD(CURDATE(),INTERVAL -12 WEEK);
https://docs.microsoft.com/en-us/sql/t-sql/language-elements/coalesce-transact-sql
The ISNULL OR IFNULL function and the COALESCE expression have a similar purpose but can behave differently.
Because ISNULL is a function, it is evaluated only once.
As described above, the input values for the COALESCE expression can be evaluated multiple times. Data type determination of the resulting expression is different. ISNULL uses the data type of the first parameter, COALESCE follows the CASE expression rules and returns the data type of value with the highest precedence.
The NULLability of the result expression is different for ISNULL and COALESCE. The ISNULL return value is always considered NOT NULLable (assuming the return value is a non-nullable one) whereas COALESCE with non-null parameters is considered to be NULL. So the expressions ISNULL(NULL, 1) and COALESCE(NULL, 1) although equivalent have different nullability values. This makes a difference if you are using these expressions in computed columns, creating key constraints or making the return value of a scalar UDF deterministic so that it can be indexed as shown in the following example.