计算两个表的列的差异

时间:2013-04-28 07:13:21

标签: mysql

我是mysql和php的新手,在进行复杂查询方面还不是很有经验。 感谢下面的stackoverflow上的一些用户查询现在成功运行。 我现在遗漏的最后一点是包括计算此查询中q1的总和与q2的成本之间的差异。感谢您的帮助。欢呼声

Select * from ( SELECT invoice.eventid, invoice.invoiceno, event.clientid, client.clientid, clientname, 
gross_amount, vat, total, due 
FROM client, invoice, event 
WHERE event.eventid = invoice.eventid 
AND event.clientid = client.clientid) 
as q1

inner JOIN (SELECT event_ma.eventid, 
salary.staffid, Sum(cost_hour * Time_to_sec(Timediff(hours, pause))) / 3600 AS costs 
FROM salary 
JOIN event_ma ON salary.staffid = event_ma.staffid GROUP BY event_ma.eventid) 
as q2  
ON q1.eventid = q2.eventid 

GROUP BY q1.eventid

1 个答案:

答案 0 :(得分:0)

在主要SELECT语句中添加计算字段:

Select *, (q1.total - q2.costs) AS difference
from ( SELECT invoice.eventid, invoice.invoiceno, event.clientid, client.clientid, clientname, 
gross_amount, vat, total, due 
FROM client, invoice, event 
WHERE event.eventid = invoice.eventid 
AND event.clientid = client.clientid) 
as q1

inner JOIN (SELECT event_ma.eventid, 
salary.staffid, Sum(cost_hour * Time_to_sec(Timediff(hours, pause))) / 3600 AS costs 
FROM salary 
JOIN event_ma ON salary.staffid = event_ma.staffid GROUP BY event_ma.eventid) 
as q2  
ON q1.eventid = q2.eventid 

GROUP BY q1.eventid