Select to bring the amount of certain records in related tables

Asked

Viewed 1,210 times

1

Speak guys, is the following, in the MYSQL database I have these 3 tables:

CONTROLS: Here I do the training entries performed. Note that it is possible to insert several employees in the same line

inserir a descrição da imagem aqui

COLLABORATORS:

inserir a descrição da imagem aqui

SECTORS:

inserir a descrição da imagem aqui

The column collaborators of the CONTROL table is a foreign key of the COLLABORATORS table. The column sectors of the COLLABORATORS table is a foreign key of the SECTORS table.

I would like a select that brings me the amount of employees per sector who participated in the training.

Would look like this:

inserir a descrição da imagem aqui

  • Friend, are you sure about this "foreign key" in the CONTROLS table? It seems to me you are storing a representation of an array in text. I may be wrong, but this is not a FK, and there would be no way you could do this query trivially with this structure.

  • Yes, it is an array, as I need to insert several contributors in the same row. These records are the contributors table Ids. It works like a FK, but in fact it is not. As far as consultation is possible yes, there must be some way. I’m trying with INNER JOIN, but I still can’t get.

  • Checks if the answer I suggested solves your problem.

3 answers

2

You can easily get this result using group by, see this example:

Select s.nome, count(s.id) as quantidade from setores s, colaboradores c where s.id = c.SETOR Group by s.id

See the example working on SQL Findle

Documentation about Group BY

  • lazyFox, I found amazing this solution, made me advance a lot. However this select is bringing me all the collaborators and registered sectors. I have 15 sectors and 256 employees. I need him to bring me only the ones who did the course, IE, only the input that was done in the table CONTROLS.

  • Either I misread the question, or you’ve done some editing :) Anyway I’ll leave here the answer that may be useful to other users. But it will be better to check the other answers because they go more according to what you want.

2

Friend, as you are probably using the JSON data type due to the contents of the CONTRIBUTORS column in the CONTROLS table. You can do it this way.. (Note that I have not tested this select, but this is the way)

SELECT 
setor.nome as setor,
count(colaborador.id) as quantidade
FROM SETORES setor
JOIN COLABORADORES colaborador 
    ON colaborador.setor = setor.id
JOIN CONTROLES controle 
    ON JSON_CONTAINS(controle.colaboradores, CAST(colaborador.id as CHAR))
GROUP BY setor.id

Since you are working with JSON, it is worth looking at some useful functions in the documentation.

Functions That Search JSON Values

0

The most complete and secure solution I could find was the following:

declare @aux_col varchar(255)
SELECT @aux_col = colaboradores FROM controles WHERE id = 1 --id do treinamento selecionado

-- esses três passos são necessários de acordo com os caracteres especiais mostrados no exemplo
select @aux_col = replace(@aux_col, '[', '')
select @aux_col = replace(@aux_col, ']', '')
select @aux_col = replace(@aux_col, '"', '')
-- esse passo é para facilitar a pesquisa, onde será sempre consultada a string ,codigo,
select @aux_col = ',' + @aux_col + ','

SELECT s.nome as setor, count(*) as quantidade
FROM colaboradores c
JOIN setores s on s.id = c.setor
WHERE LOCATE(',' + c.id + ',', @aux_col) > 0
GROUP BY s.nome
ORDER BY s.nome

The validation in locate using comma before and after the id is necessary because if it did not exist, searching by code 1 would return truth if there was code 11, for example.

  • Note that he requested the number of servers, grouped by sector, that participated in a training. This requires the relationship with the CONTROL table. Your query returns only the number of employees per sector.

  • Putz, you’re right. Activate the last two tables.. I’ll fix the answer. Thank you, @Israelmerljak

  • Dude, never use a Count(*), it ends up weighing on the query in a very populated bank. Always specify a field in the Count...

  • For real, for that matter performance there is no difference between one or the other approach (ex: that treatment), the fact that it counts between *, campo or 1 (for example) would weigh in the count of null values, which in this case would not differentiate. (@Victorlaio, if I said nonsense, correct me =p )

  • 1

    This can vary too much from bank to bank, because there are n factors that will influence. In my experience where I worked there were cases where there was a small performance gain making the modification. But if you want to know more I recommend: https://stackoverflow.com/questions/2710621/count-vs-count1-countpk-which-is-better/2710703#2710703

Browser other questions tagged

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