I have a table that looks like this
CREATE TABLE `purchases` (
`id` INT(10) NOT NULL AUTO_INCREMENT,
`totalAmount` INT(10) NOT NULL DEFAULT '0',
`purchaseDate` DATETIME NOT NULL,
PRIMARY KEY (`id`)
)
and another table that stores all the details of the purchases (eg.line items)
CREATE TABLE `purchase_items` (
`id` INT(10) NOT NULL AUTO_INCREMENT,
`purchaseId` INT(10) NOT NULL,
`itemId` INT(10) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
FOREIGN KEY (`purchaseId`) REFERENCES purchases(id)
)
I get the total purchases on any given day this way
SELECT DATE_FORMAT(purchaseDate, '%m-%d-%Y') AS date,
COUNT(id) AS totalPurchases
FROM purchases
GROUP BY DATE(purchaseDate)
This gives me a result as such
date totalPurchases
11-18-2010 5
11-19-2010 10
11-20-2010 10
How will I perform a query such that it returns me something like this
date totalPurchases totalItems
11-18-2010 5 20
11-19-2010 10 30
11-20-2010 10 25
So I will still be grouping by date but I need to perform a join on 'purchase_items' and get the total number of item that were linked to the
- 5 Purchases on 11-18-2010
- 10 Purchases on 11-19-2010
- 10 Purchases on 11-20-2010