Mysql返回0并非null

时间:2015-08-19 10:48:33

标签: mysql null aggregate-functions

需要帮助请我有一个包含taskId,Materials,Labor和带有费用的表的表。我遇到的问题是taskenpense表中的某些任务没有和expense列,所以该列返回null。我需要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)

我需要null值返回0,因此总和可以计算出来  FIDDLE LINK 谢谢 乔恩

2 个答案:

答案 0 :(得分:1)

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列上,请将其更改为您想要的内容。

祝你好运。

答案 1 :(得分:1)

比@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