-1

I have two tables and i want to add there row based on its name. I've search on net but I only found how to combine the total value or two tables and combine them. The result will be added on a table named Result

     Table 1               Table 2              Result
Name    |  Value         Name   | Value       Name  |  Value
Apple   |    2           Apple  |   4         Apple |    6
Orange  |    3           Orange |   2         Orange|    5

Thank you in advance

  • 3
    Possible duplicate of [SQL JOIN and different types of JOINs](https://stackoverflow.com/questions/17946221/sql-join-and-different-types-of-joins) – Laposhasú Acsa Sep 06 '17 at 09:23

1 Answers1

0

First of all I would like to say you must try to get solution at your own.

For your case, answer is very simple. Try this query :

SELECT table1.`name`, (table2.value + table1.value) AS `value` FROM table1
LEFT JOIN table2 ON table1.`name` = table2.`name` WHERE table1.`name` = table2.`name`
Mayank Jain
  • 369
  • 5
  • 21
  • Yes sir im looking for a solution but can't find what I'm looking. Thanks for ur solution. :D – tryingtobebest Sep 06 '17 at 09:28
  • Solution will work in this case but it may not be that simple what if table2 has more entries than table1 or a null is encountered because there is no match found? – P.Salmon Sep 06 '17 at 09:29
  • @P.Salmon, I just updated my answer for the case you are talking about. Rest it depends upon requirement that what result he wants in this case. – Mayank Jain Sep 06 '17 at 09:37