How to get the sum with more then one table I have 3 tables as shown in the link sqlfiddle
When I take the sum with two tables I get good result 
    sqlfiddle

but when I add 3rd table and it's query I get unexpected result as this

http://sqlfiddle.com/#!2/f9858a/12

How can I resolve this problem pls ?
When you do the aggregation without ingredient, then LEFT JOIN result to ingredient you get this

mysql&gt; SELECT A.*,ig.qty FROM
    -&gt; (SELECT dm.id,dm.nom,
    -&gt; SUM(IF(dm.id=sd.matid, quantite,0)) as thisqty,
    -&gt; SUM(IF(dm.id=sd.matid, sd.montant * (1+sd.tvaval/100),0)) AS thissum
    -&gt; FROM stockdata AS sd
    -&gt; JOIN defmatiere as dm ON dm.id=sd.matid
    -&gt; WHERE dm.type=1 GROUP BY dm.nom ORDER BY dm.id) A
    -&gt; LEFT JOIN ingredient as ig ON ig.matid=A.id;
+----+----------+---------+--------------------+------+
| id | nom      | thisqty | thissum            | qty  |
+----+----------+---------+--------------------+------+
|  1 | Amande   |   19470 | 204.74464999999998 |  600 |
|  1 | Amande   |   19470 | 204.74464999999998 |  200 |
|  2 | Chocolat |    7920 |              62.97 |  100 |
|  2 | Chocolat |    7920 |              62.97 |  200 |
|  4 | Beurre   |     500 |               2.48 |   60 |
|  4 | Beurre   |     500 |               2.48 |   60 |
+----+----------+---------+--------------------+------+
6 rows in set (0.00 sec)

mysql&gt;


Perhaps doing an aggregation after the INNER JOIN would help

mysql&gt; SELECT A.*,SUM(ig.qty) igqty FROM
    -&gt; (SELECT dm.id,dm.nom,
    -&gt; SUM(IF(dm.id=sd.matid, quantite,0)) as thisqty,
    -&gt; SUM(IF(dm.id=sd.matid, sd.montant * (1+sd.tvaval/100),0)) AS thissum
    -&gt; FROM stockdata AS sd
    -&gt; JOIN defmatiere as dm ON dm.id=sd.matid
    -&gt; WHERE dm.type=1 GROUP BY dm.nom ORDER BY dm.id) A
    -&gt; LEFT JOIN ingredient as ig ON ig.matid=A.id
    -&gt; GROUP BY A.id,A.nom,A.thisqty,A.thissum;
+----+----------+---------+--------------------+-------+
| id | nom      | thisqty | thissum            | igqty |
+----+----------+---------+--------------------+-------+
|  1 | Amande   |   19470 | 204.74464999999998 |   800 |
|  2 | Chocolat |    7920 |              62.97 |   300 |
|  4 | Beurre   |     500 |               2.48 |   120 |
+----+----------+---------+--------------------+-------+
3 rows in set (0.00 sec)

mysql&gt;