S
S
Silverius162019-11-21 16:05:04
MySQL
Silverius16, 2019-11-21 16:05:04

How can I sum columns from 2 tables and display in the summary?

There is a PRIC 5dd6891c18c0a340677423.pngtable and an
AMO table. 5dd68990e70f0910938414.png
How to write a query that displays the following information from the PRIC and AMO tables:
Employee name; The total amount paid (the amount from the first table and the second)
But if there were no payments for one of the employees, then 0 should be indicated in the field with the total amount paid.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
idShura, 2019-11-21
@Silverius16

SELECT a.name
       sum(a.zarp)
  FROM (SELECT name, 
               IFNULL(zarp, 0) zarp 
  	      FROM PRIC
        union all
        SELECT name, 
               IFNULL(amount, 0) zarp 
          FROM AMO
       ) A
 GROUP BY a.name

K
Konstantin Tsvetkov, 2019-11-21
@tsklab

SELECT PRIC.Name, PRIC.Zarp + ( SELECT SUM( Amount ) FROM AMO WHERE AMO.Name = PRIC.Name ) FROM PRIC

R
res2001, 2019-11-21
@res2001

something like this:

select Name, sum(Amount) from (
  select Name, Amount from AMO
  union all
  slect Name, Zarp as Amount from PRIC
) group by Name

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question