这是合约。
我有两张表,每小时费率和单位费率由phase_no数字表示。在一张桌子上它可能有第2,3,14和81阶段,在另一张桌子上有2,14,20,83我需要将表1 sum()添加到表2中,但是如果你看下面的结果,你会看到虽然我打电话给coalese它仍然产生NULL,你不能应用数学。希望你能看到我得到的东西。
SELECT ru.phase_no,
ROUND(SUM(ru.units * ru.rate) * 0.75, 2) , (SELECT
ROUND(SUM(rh.hours * rhh.rate) * 0.75, 2)
FROM
rates_hourly_required rh
JOIN
rates_hourly rhh ON rhh.id = rh.rates_hourly_id
WHERE
rh.project_id = ru.project_id and rh.phase_no =ru.phase_no group by rh.phase_no,rh.project_id)
FROM
rates_unit_required ru
WHERE
ru.project_id = 67320
group by ru.project_id, ru.phase_no
也许有更好的方法来处理此查询。
答案 0 :(得分:0)
使用LEFT JOIN
而不是相关的子查询
SELECT ru.phase_no,
ROUND(SUM(ru.units * ru.rate) * 0.75, 2) AS ru_total,
IFNULL(rh_total, 0) AS rh_total
FROM rates_unit_required AS ru
LEFT JOIN (
SELECT rh.phase_no, ROUND(SUM(rh.hours * rhh.rate) * 0.75, 2) AS rh_total
FROM rates_hourly_required rh
JOIN rates_hourly rhh ON rhh.id = rh.rates_hourly_id
WHERE rh.project_id = 67320
GROUP BY rh.phase_no
) AS rh ON rh.project_id = ru.project_id AND rh.phase_no = ru.phase_no
WHERE ru.project_id = 67320
GROUP BY ru.phase_no
project_id
当您在WHERE
子句中选择特定值时,"a": "str"
分组也毫无意义。