Do not repeat data when making a SELECT in Mysql

Asked

Viewed 4,451 times

3

What I want is this. I’m working on a system, which registers an employee and days that he worked on during the month. So far so good, but it happens that in some cases, the employee gets 2 or more records in the same month, for example, I registered, between days 21 e 25 of May, and 28 e 30 of May. Right on my table there are the following records:

1 - Funcionario1 - 5 (dias) - 5 (mês de maio)
2 - Funcionario1 - 3 (dias) - 5 (mês de maio)

All right, now what I need to do is calculate the total number of days in the middle of May that this employee worked. I get more or less with this code:

for($i=0; $i<$mesatual; $i++) {
            echo "<h2>Plantões / ".$meses[$i]."</h2><br><br>";

            $query = "SELECT diasplantao.*, funcionarios.nome, funcionarios.atribuicao FROM diasplantao "
                    . "INNER JOIN funcionarios ON (diasplantao.funcionario_id = funcionarios.funcionario_id) WHERE (diasplantao.mes = $i+1) "
                    . "ORDER BY funcionarios.atribuicao DESC";

            $resultado = mysql_query($query);

            while($mostrar=mysql_fetch_array($resultado)) {
                echo "<b>Nome:</b> ".$mostrar['nome']." -- ";
                echo "<b>Atribuição:</b> ".$mostrar['atribuicao']." -- ";
                echo "<b>Mês:</b> ".$mostrar['mes']." -- ";
                echo "<b>Dias:</b> ".$mostrar['dias']."<br><br>";

            }
        }  
    ?>

The code shows the month, and just below the days the employees worked that month. Only that it returns me this, in the month of March for example.

Nome: George Cruijff Sales da Costa -- Atribuição: Analista -- Mês: 3 -- Dias: 12

Nome: George Cruijff Sales da Costa -- Atribuição: Analista -- Mês: 3 -- Dias: 10

How do I not repeat the name, and how do I best calculate the total days? I was thinking of just putting an auxiliary variable and adding, but the name will keep repeating.

Someone lights up the best way to do it?

  • Ever tried to give a group by in the funcionarios.nome?

  • I used GROUPY BY and got it, but now as I do to make the sum, I don’t know if my code got confused, but I can’t do the sum.

  • And if there are two 'João da Silva' working in the company?

  • I don’t have to worry about it because I know it won’t happen. But as a precaution I grouped by employee id. The problem even now is just the sum. I can’t make a mistake in this sum at all.

  • Beware that it can happen yes (you never know). But the ideal is to group by some property that is unique (id serves).

  • Grouped by id already. Thanks =D

Show 1 more comment

1 answer

1


You must use the SUM to sum up the days and the GROUP to bring together the grouped lines

SELECT diasplantao.mes, 
       SUM(diasplantao.dias) as dias,
       funcionarios.id,
       funcionarios.nome, 
       funcionarios.atribuicao 
FROM 
    diasplantao 
    INNER JOIN funcionarios ON (diasplantao.funcionario_id = funcionarios.funcionario_id) 
WHERE 
    (diasplantao.mes = $i+1)
GROUP BY
    funcionarios.id, diasplantao.mes, funcionarios.nome, funcionarios.atribuicao 
ORDER BY 
    funcionarios.atribuicao DESC
  • It was perfect, thank you. Just one last question, why use groupby for month, name, and attribution too? Only id not enough? Thanks!

  • Firebird used to complain about campo X não é agregatte e não está presente na cláusula GROUP BY From there I created the practice of grouping all the uncompleted fields. I don’t know if Mysql wouldn’t complain, but to have a standard SQL I ended up adopting this habit. So, if Mysql does not complain, I believe there is no problem

  • Got it, thanks! Thanks.

Browser other questions tagged

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