Forum Moderators: coopster
basically what I need is this
product ID ¦ YTD units ¦ MTD units ¦ Las update
28737 2 1 2009-05-12
28630 1 1 2009-05-12
I have two similar queries,
One to calculate the YTD units
SELECT sales.idsales, sales.date_sales, products.idProducts, SUM(receipts.qty_receipt)
FROM sales, products, receipts
WHERE Year(date_sales)=Year(curdate()) AND receipts.idsales=sales.idsales AND products.idProducts=receipts.idProducts
GROUP BY products.idProducts
And one to calculate the MTD units
SELECT sales.idsales, sales.date_sales, products.idProducts, SUM(receipts.qty_receipt)
FROM sales, products, receipts
WHERE MONTH(date_sales)=MONTH(curdate()) AND receipts.idsales=sales.idsales AND products.idProducts=receipts.idProducts
GROUP BY products.idProducts
Does anyone know how to combine those two queries to be able to display them horizontaly in the same table? So I can show the sales per year and sales per month for each product?
Thank you
basic idea:
$array = array(); // initialize array
while($row_ytd = mysql_fetch_assoc($query1)) {
$array[$row_ytd['product_id']]['ytd'] = $row_ytd['ytd'];
$array[$row_ytd['product_id']]['last_update'] = $row_ytd['last_update'];
}
while($row_mtd = mysql_fetch_assoc($query2)) {
$array[$row_mtd['product_id']]['mtd'] = $row_mtd['mtd'];
}
from there use foreach [php.net] to put the results into a table