How to add multiple rows of two tables in Mysql

Asked

Viewed 837 times

3

I’ve tried this code, but it only adds up to one line, if you modify the where, error or wrong sum, need to add each separate line.

I need to add these two fields with several lines.

  • km_h_tp is the table field t_trocaprogramadas

  • km_h_pl is the table field t_planos

sql:

select (select sum(km_h_tp) from t_trocaprogramadas where fkplanos_tp =1) +
(select sum(km_h_pl) from t_planos where idt_plano =1)'Proxima Troca';
  • 2

    I formatted your question to make it more readable, but you still need to [Edit] and explain better what you want. It is not possible to know exactly what it is to add to what, what you call separate lines, etc. It would be better to add an example of how your data is, and how the result should look.

  • Post also the DDL of the tables to know how the two tables relate.

1 answer

1


You need to edit your question with more details of your tables, if what other way to do this could be, of course I don’t know what you really filter.

It would be interesting for you to see a little about INNER JOIN , SUM and a little bit of WHERE .

select (sum(km_h_tp) +  sum(km_h_pl)) as 'Proxima Troca'
from t_trocaprogramadas tp
join t_planos  p
on p.idt_plano = 1
and fkplanos_tp = 1;
  • 1

    ok thanks friend with a small modification worked, see code......................................................................................................select and.apelido_e, p.descricao_pl, (sum(t.km_h_tp) + sum(p.km_h_pl)) as 'Next Exchange' from t_trocaprogramadas as t inner join t_planos as p on (t.fkplano_tp = p.idt_plano)
inner join t_equipamentos as e on (t.fkequipamento_tp = e.idt_equipamento)
where t.fkequipamento_tp =22 group by p.idt_plano;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.