MySQL值显着膨胀

时间:2014-03-23 12:14:12

标签: php mysql sql sum

我有一个查询,旨在获取当月每一天的查询值。

我遇到的问题是数字太大了。

这是我的问题:

$M = date('m');
$Y = date('Y');
$SQL = "SELECT DATE_FORMAT(enquiries.dateCreated, '%D') AS name,
            SUM((SELECT SUM(PE.quantity * PE.net) FROM parts_enquiries PE WHERE PE.enquiryId = enquiries.id)) AS y
            FROM enquiries
            WHERE MONTH(enquiries.dateCreated) = $M
            AND YEAR(enquiries.dateCreated) = $Y
            AND enquiries.archived = 0
            GROUP BY DAY(enquiries.dateCreated)";

每天可以进行0次或更多次查询,每次查询可以有1个或更多部分。

有谁知道我的查询出错了?

1 个答案:

答案 0 :(得分:0)

我不知道这是否有所作为,但尝试使用显式join s,没有子查询,没有隐式连接来运行它:

SELECT DATE_FORMAT(enquiries.dateCreated, '%D') AS name,
       SUM(PE.quantity * PE.net) AS y
FROM enquiries JOIN
     entities
     ON enquiries.entityId = entities.id JOIN
     parts_enquiries PE
     ON PE.enquiryId = enquiries.id
WHERE MONTH(enquiries.dateCreated) = $M AND
      YEAR(enquiries.dateCreated) = $Y AND
      enquiries.archived = 0
       $filter
GROUP BY DAY(enquiries.dateCreated);

一种可能性是将$filter设置为其中包含OR的内容。写这个更安全的方法是:

WHERE MONTH(enquiries.dateCreated) = $M AND
      YEAR(enquiries.dateCreated) = $Y AND
      enquiries.archived = 0
      (1=1 AND $filter)