我想创建一个名为WORKER的表,他建造的Square Meteres总数以及他支付的总费用。
我有2张桌子
tbl_orden和tbl_costos
obrero是tbl_costos中的外键
这是我尝试的查询:
SELECT a.obrero AS MAESTRO, ROUND(SUM(metros_c),2) AS METROS, SUM(b.costo_semanal)
FROM tbl_orden as a, tbl_costos as b GROUP BY a.obrero
我尝试上面的查询,但我没有得到正确的结果。 metros_c的SUM不是costo_semanal的总和
tbl_order
tbl_costos:
我想要这个查询结果:
SELECT obrero AS Maestro, ROUND(SUM(metros_c),2) AS Metros_Cuadrados, ROUND(SUM(metros_c)/MONTH(CURDATE()),2) as Promedio_Mensual, ROUND(SUM(metros_c)/WEEK(CURDATE()),2) as Promedio_Semanal FROM `tbl_orden`
WHERE dated BETWEEN '01/01/2015' AND '01/01/2016' GROUP BY obrero ORDER BY metros_c asc
并且此查询会生成上表的其他列:
SELECT SUM(costo_semanal) from tbl_costos GROUP BY obrero
第一个查询我得到4列,第二个得到1.我希望得到一个包含5列的结果表
答案 0 :(得分:0)
您可以将每个放入内联视图,然后将它们连接起来执行此操作:
select x.*,
y.sum_costo_semanal
from (select obrero as maestro,
round(sum(metros_c), 2) as metros_cuadrados,
round(sum(metros_c) / month(curdate()), 2) as promedio_mensual,
round(sum(metros_c) / week(curdate()), 2) as promedio_semanal
from tbl_orden
where dated between '01/01/2015' and '01/01/2016'
group by obrero) x
join (select obrero,
sum(costo_semanal) as sum_costo_semanal
from tbl_costos
group by obrero) y
on x.obrero = y.obrero