How to automatically add two columns from two different tables and perform Insert in another table in Mysql?

Asked

Viewed 1,469 times

2

I am starting work with database and would like to perform an intelligent action in the bank.

I have two tables that store information from different sensors, each with its respective columns.

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

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

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

This action could be run through a Trigger after inserting some data in tables T1 and T2 and thus adding the added row data.

I would like some hint as to how this can be accomplished.

Thank you.

  • Do you want to run this directly in a database query or are you going to use something for it? (some site, program...)

  • I’ll use it right at the bank, so it controls the entrances.

2 answers

2

What is the purpose if the records of the tables do not have a relationship with each other?

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

This way, you can make the sum something like:

SELECT T1.COLUNA1_T1 + T2.COLUNA1_T2 as COLUNA1_T3 FROM T1 INNER JOIN T1 ON T1.CHAVE = T2.CHAVE
  • The only relation that they carry are in question to the identifier of who is entering the information where technically both would be inserted at the same time, even if it is different information.

  • In "ON T1.KEY = T2.KEY" the key cannot be the insertion ID of each one?

1

I minimized my problem. I joined the two tables, after all only one column alternated and they were inserted automatically. Now I created a Trigger in the bank to generate the sum automatically:

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

Thank you for the reply.

Browser other questions tagged

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