I have two different tables a and ah.
a is the article stock quantity, ah is the article stock movement.
I would like to have all the rows from a and only the current date (today) movement row from ah and add two columns. I’ve tried a few things, one of them being right join but I only get articles that had a movement today. Please Could you help?
I’ve tried this:
SELECT a.numero,
a.stockDay,
date(ah.dateCreation),
ah.creationDate,
SUM(a.stockDay + ah.diffQuantite) AS 'stockReel'
FROM article a, articleLotHisto ah
WHERE ah.articleId = a.id
AND date(ah.dateCreation) = CURDATE();
Also that:
SELECT a.numero,
a.stockDay,
date(ah.dateCreation),
ah.dateCreation,
SUM(a.stockDay + ah.diffQuantite) AS 'stockReel'
FROM article a
RIGHT JOIN articleLotHisto ah
ON ah.articleId = a.id
WHERE date(ah.dateCreation) = CURDATE();
But I always get only the rows that have been created today and not the complete a table.
2
Answers
I've just added a "sum" and a "group by", because we can have more than one movement per day and we want them to be sum up. Here is the query you gave me. Line 18,19,20 is the same article number with each time a -2 quantity move result is 58 it should be 54enter image description here:
Here I added a sum and a group by the result is correct. Thanks again for your help :-) enter image description here
Filtering table ah on the date BEFORE joining it to a should do what you want; try this:
Edit: added
COALESCE
for articles with no movement today.