MYSQL LEFT JOIN BRINGS WRONG RESULTS -
MYSQL LEFT JOIN BRINGS WRONG RESULTS -
i trying figure out why bring together brings results does.
here simple test im working on.
i have 2 tables : table1 , table2.
table1
------------------- id name amount --------------------- 1 | fred | 2 3 | fred | 3 5 | fred | 4 2 | hellen | 3 4 | hellen | 3 6 | hellen | 3
table2
------------------- id name amount --------------------- 1 | fred |4 2 | hellen |3 3 | paul |3 4 | fridah |5
when utilize next statement
select p.name,sum(p.amount) amount `table1` p left bring together table2 c on p.name = c.name grouping c.name , p.name
i result
--------------- name amount --------------- fridah | 5 paul | 3 fred | 12 hellen | 9
but wierd. want sum of amount
table1, b ut bring together gives me sum of both tables. why this?
this query:
select p.name, sum(p.amount) amount `table1` p left bring together table2 c on p.name = c.name grouping c.name , p.name;
your results suggest query running is:
select p.name, sum(c.amount) amount -------------------^ `table1` p left bring together table2 c on p.name = c.name grouping c.name , p.name;
if run right query, results expect.
edit:
the right query like:
select c.name, sum(p.amount) amount table2 c left bring together table1 p on p.name = c.name grouping c.name;
mysql left-join
Comments
Post a Comment