2013-04-28 89 views
0

我是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 
+0

不要使用 「SELECT *」。寫出你真正想要返回的列的名字......「SELECT q1.id,等等......」。那麼解決您的問題可能會奇蹟般地發生在您身上! – Strawberry 2013-04-28 07:18:58

回答

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 
+0

這是完美的。謝謝。最後很容易。 – 2013-04-28 07:32:46