1
Good afternoon, I am very new in MYSQL, basically I am learning in network searches, but a question arose that I could not solve.
I have about 6 tables, in each I have 10 fixed and identical entries in the field ID and TYPE, which are not changed, being possible to change only the values of these fields.
Example:
Dep1
ID TIPO VALOR1 VALOR2 VALOR3 VALOR4
1 Camisa 10 20 30 40
2 Calça 15 25 35 45
3 Blusa 3 2 9 25
Dep2
ID TIPO VALOR1 VALOR2 VALOR3 VALOR4
1 Camisa 5 10 15 3
2 Calça 30 5 5 10
3 Blusa 9 1 5 5
Dep3
ID TIPO VALOR1 VALOR2 VALOR3 VALOR4
1 Camisa 5 10 15 3
2 Calça 30 5 5 10
3 Blusa 9 1 5 5
E assim por diante....
The idea is to be able to mount a query to sum all the values per item, of each table, getting the following result:
deptotal (id, tipo, total1,total2,total3,total4,totalgeral)
ID TIPO TOTAL1 TOTAL2 TOTAL3 TOTAL4 VALORTOTAL
1 Camisa 20 40 60 46 166
2 Calça 75 35 45 65 220
3 Blusa 21 4 19 35 79
I tried this way, but with a terrible "query time", getting to crash the server for a few moments:
select
(dep1.id) as ID,
(dep1.tipo) as TIPO,
(dep1.valor1)+(dep2.valor1)+(dep3.valor1)+(dep4.valor1)+(dep5.valor1)+(dep6.valor1) as total1,
(dep1.valor2)+(dep2.valor2)+(dep3.valor2)+(dep4.valor2)+(dep5.valor2)+(dep6.valor2) as total2,
(dep1.valor3)+(dep2.valor3)+(dep3.valor3)+(dep4.valor3)+(dep5.valor3)+(dep6.valor3) as total3,
(dep1.valor4)+(dep2.valor4)+(dep3.valor4)+(dep4.valor4)+(dep5.valor4)+(dep6.valor4) as total4,
(dep1.valor1)+(dep2.valor1)+(dep3.valor1)+(dep4.valor1)+(dep5.valor1)(dep6.valor1)+(dep1.valor2)+(dep2.valor2)+(dep3.valor2)+(dep4.valor2)(dep5.valor2)+(dep6.valor2)+ (dep1.valor3)+(dep2.valor3)+(dep3.valor3)+ (dep4.valor3)+(dep5.valor3)+(dep6.valor3)+(dep1.valor4)+(dep2.valor4)+ (dep3.valor4)+(dep4.valor4)+(dep5.valor4)+(dep6.valor4) as VALORTOTAL
FROM
dep1,dep2,dep3,dep4,dep5,dep6
where (dep1.id = dep2.id = dep3.id = dep4.id = dep5.id = dep6.id)
Good friends, maybe the table was very confusing, but in the end it was functional, presenting problems when I added more tables in the database.
Any help is welcome, hug.
Do you define the database? Why not add a "department" column and use a single table?
– jsbueno
i define only the ID, which refers to an internal code, and the TYPE, which is standard, the idea would be to change only the values.
– Vinicius
But you created the system? If yes why have a table for each department, it makes no sense, and if you need to create a new department will do what, create one more table?
– Leandro Godoy Rosa
I created it, but like I said, I have no knowledge of it, and I’m the basis of research starting to learn. I’ll try to set another table the way you advised
– Vinicius