Reputation: 929
i need some help with this SQL Join..
I have a table "sales" , looking like this
date idpart
1311316400 5
1321316400 6
1341216400 7
1351516400 8
and a table "parts" looking like this
idpart name inprice outprice
5 a 10 40
6 b 20 55
7 c 20 55
8 d 20 55
Now i want to find the sum of all partsales between a user given date A and date B , by doing something like this:
SELECT idpart, SUM( inprice )
FROM parts, sales
WHERE parts.idpart = sales.idpart
GROUP BY idpart
LIMIT 0 , 30
But by doing this i get the value of inprice for each of the time a part is in sales table. I want the total for the entire period between A and B. I dont know where i should insert the :
WHERE date BETWEEN A AND B
any tips for a SQL beginner ?
Upvotes: 1
Views: 66
Reputation: 55584
If I got your question right, then you can just combine your conditions using the keyword AND
:
SELECT SUM( inprice )
FROM parts, sales
WHERE parts.idpart = sales.idpart
AND sales.date BETWEEN A AND B
LIMIT 0 , 30
For better readability always use the "new" JOIN
-syntax. Once your queries become more complex, this makes it a lot easier to understand your queries:
SELECT SUM( parts.inprice )
FROM sales
JOIN parts ON ( parts.idpart = sales.idpart )
WHERE sales.date BETWEEN a AND b
LIMIT 0, 30
Upvotes: 3