2016-01-20 93 views
-1

我有兩個表「發票」和「invoices_account」選擇總和

通過查詢

SELECT DATE_FORMAT(inv_created, '%Y') as 'year', 
     COUNT(*) as 'total' 
FROM invoices as i 
WHERE inv_way = "OUT" 
GROUP BY DATE_FORMAT(inv_created, '%Y') 

我拿到發票的金額,每年這樣的:

2011 | 80 
2012 | 120 

等。

我需要的是在第一個查詢中,按年份從「invoices_account」。「inv_total」中獲取所有條目的摘要。關係是1:1。這意味着2011年的80份發票在「invoices_account」中有80行。我需要有這樣的事情

2011 | 80 | 1.000.000 € 
2012 | 120 | 230.000.000 € 

回答

1

檢查INNER JOIN

SELECT DATE_FORMAT(inv_created, '%Y') as 'year', 
     COUNT(*) as 'total', 
     SUM(ia.inv_total) as money_total 
FROM invoices as i 
JOIN invoices_account ia 
    ON i.id = ia.id 
WHERE inv_way = "OUT" 
GROUP BY DATE_FORMAT(inv_created, '%Y')