Mysql return 0 总和不为空

Mysql return 0 not null in sum

需要帮助我有一个 table 包含 taskId、材料、劳动力和一个 table 包含费用。我遇到的问题是有些任务在 taskenpense 中没有和费用列table 所以列 return 为空。我需要 null 为 0.

       ` CREATE TABLE emptasks (  empTaskId INT,  taskMaterials NUMERIC(8,2),taskLabour NUMERIC(8,2));
        INSERT INTO emptasks VALUES
      (1,  50, 50),
      (2,   450.26, 50),
      (3,  2505.10, 50),
       (4, 2505.10, 50),
       (5, 500, 500),
       (6, 1000, 50);


    CREATE TABLE taskexpenses (
      feeID INT,
      empTaskId INT,
      expense NUMERIC(8,2));

    INSERT INTO taskexpenses VALUES
    (1, 1,  50.00),
      (1, 2,  50.00),
      (2, 2, 126.00),
      (3, 3,  50.00),
      (4, 4,  50.00),
      (2, 2, 1206.00);

SELECT
    p.empTaskId,
      p.Labour,
      p.Materials,
      f.Expenses,
      p.Labour + p.Materials - f.Expenses AS Total,
   ROUND( (f.Expenses  + p.Materials) / p.Labour * 100, 2) AS Percentage
    FROM (
      SELECT
      empTaskId, 
        SUM(taskMaterials) AS Labour,
      SUM(taskLabour) AS Materials
      FROM emptasks
      GROUP BY empTaskId
    ) p
    LEFT JOIN (
      SELECT taskexpenses.empTaskId,

      SUM(expense) AS Expenses
      FROM emptasks
      INNER JOIN taskexpenses ON emptasks.empTaskId = taskexpenses.empTaskId
      GROUP BY empTaskId
    ) f ON p.empTaskId = f.empTaskId

the result is
empTaskId   Labour  Materials   Expenses    Total   Percentage
1            50        50        50          50      200
2          450.26      50       1382        -881.74 318.04
3          2505.1      50       50          2505.1  3.99
4          2505.1      50       50          2505.1  3.99
5           500         500    (null)      (null)   (null)
6          1000        50      (null)      (null)   (null)

我需要 return 0 的 null 值,以便计算总和 FIDDLE LINK 谢谢 乔恩

Use the COALESCE function:

SELECT p.empTaskId,
       p.Labour,
       p.Materials,
       COALESCE(f.Expenses, 0) AS Expenses,
       COALESCE(p.Labour, 0) + COALESCE(p.Materials, 0) - COALESCE(f.Expenses, 0) AS Total,
      ROUND( (COALESCE(f.Expenses, 0) + COALESCE(p.Materials, 0)) / p.Labour * 100, 2) AS Percentage
  FROM (SELECT empTaskId, 
               SUM(COALESCE(taskMaterials, 0)) AS Labour,
               SUM(COALESCE(taskLabour, 0)) AS Materials
          FROM emptasks
          GROUP BY empTaskId) p
  LEFT JOIN (SELECT taskexpenses.empTaskId,
                    SUM(COALESCE(expense, 0)) AS Expenses
               FROM emptasks
               INNER JOIN taskexpenses
                 ON emptasks.empTaskId = taskexpenses.empTaskId
               GROUP BY empTaskId) f
    ON p.empTaskId = f.empTaskId

请注意,在这里我已经将 COALESCE 放在几乎所有可能为 NULL 的对象上。如果您只想将其放在 Expenses 列中,请将其更改为您想要的内容。

祝你好运。

比@Bob Jarvis 的回答稍微简单一点就是使用 IFNULL() 函数。

SELECT
    p.empTaskId,
      p.Labour,
      p.Materials,
      IFNULL(f.Expenses, '0') AS Expenses,
      IFNULL(p.Labour + p.Materials - f.Expenses, '0') AS Total,
   IFNULL(ROUND( (f.Expenses  + p.Materials) / p.Labour * 100, 2), '0') AS Percentage
    FROM ...

fiddle