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
?– Erlon Charles
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.
– João Josino
And if there are two 'João da Silva' working in the company?
– Lucas Lima
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.
– João Josino
Beware that it can happen yes (you never know). But the ideal is to group by some property that is unique (id serves).
– Lucas Lima
Grouped by id already. Thanks =D
– João Josino