I have a sales table, with fields product and cost of type varchar and decimal.
I also have a purchases table, with fields product and cost, also of type varchar and decimal.
lastly, I have a products table, with fields name, category and cost, of type varchar, varchar and decimal.
I am doing the following query to get the NET income
SELECT Q1.total_sales - Q2.total_purchases AS NET
FROM (select SUM(sales.cost) AS total_sales
from sales) Q1,
(select SUM(purchases.cost) AS total_purchases
from purchases) Q2
How can I relate it back to products in the products table, to only select certain products where category = blah?
Would this have to be a union?
I would also like to show this for the current year or month, to do so would I select the date field and just append
WHERE MONTH( s.saledate ) = MONTH( NOW( ) )
to my query?