How to automatically add two columns from two different tables and perform insert into another table in MySQL?

2

I'm starting the database work and would like to do some smart action on the database.

I have two tables that hold different sensor information, each with its own columns.

I would like to get a column of each table (of my choice) and perform a sum of both, as the select below:

SELECT (SELECT COLUNA1_T1 FROM T1) + (SELECT COLUNA1_T2 FROM T2) as COLUNA1_T3

My result in COLUMN1_T3 would be the sum of the first of a row of columns, for example.

This action could be run through a trigger after insertion of some data in the T1 and T2 tables and thus add the data of the added line.

I would like some suggestion of how this can be accomplished.

Thank you.

    
asked by anonymous 01.05.2018 / 05:07

2 answers

2

What is the purpose if the records of the tables have no relation to each other?

To relate the 2 tables, they must have a common key field for you to declare in the INNER JOIN other any JOIN.

In this way, you will be able to do something like this:

SELECT T1.COLUNA1_T1 + T2.COLUNA1_T2 as COLUNA1_T3 FROM T1 INNER JOIN T1 ON T1.CHAVE = T2.CHAVE
    
01.05.2018 / 06:01
1

I minimized my problem. I joined the two tables, after all only one column alternated and they were inserted automatically. Now I've created a trigger on the database to generate the sum automatically:

set new.T3 = (new.T1 + new.T2)

Thanks for the response.

    
01.05.2018 / 07:36