Return of Equal Employee

Asked

Viewed 26 times

0

I have the following code

$sql = "
        SELECT 
            c.*, 
            cl.razaosocial, 
            cl.idCliente 
        FROM 
            chamada as c, 
            cliente as cl 
        WHERE 
            c.idCliente = cl.idCliente";
$consulta = $this->db->query($sql)->result();

foreach($consulta as &$valor)
{
    $this->db->where('idFuncionario', $valor->idFuncionario);
    if($this->db->get('funcionario')->row('nome'))
    {
        $valor->funcionarioNome = $this->db->get('funcionario')->row('nome');
    }
    else 
    {
        $valor->funcionarioNome = "";   
    }
}

return $consulta;

He returns in the $valor->funcionarioNome always the same name.

What would have been wrong there?

I thought to try to make this SQL, it only displays those who are set the employees in the call, but there are calls in "open" no elected officials yet.

SELECT c.*, f.nome as nomeFuncionario, f.idFuncionario, cl.razaosocial, cl.idCliente FROM chamada as c, cliente as cl, funcionario as f WHERE c.idCliente = cl.idCliente AND c.idFuncionario = f.idFuncionario GROUP BY c.idFuncionario

Esta imagem representa o erro. Notem que por mais que o ID funcionario seja diferente, ele imprime nomes iguais...

  • $valor should be an array of functionalities or just a?

  • Just a clerk, actually, I just want his name...

  • Edit your question, describe what you want to do. Just by looking at the code it does not 'demonstrate' any intention.

  • I added the information

1 answer

2


If you want to add the employee name on the return of the query is not easier to get it on SELECT?

I believe that your idFuncionario is on the table Chamada, then:

$sql = "SELECT DISTINCT c.*, 
               cl.razaosocial, 
               cl.idCliente,
               f.nome as funcionarioNome
            FROM chamada as c
            INNER JOIN cliente as cl on c.idCliente = cl.idCliente
            LEFT JOIN funcionario as f on c.idFuncionario = f.idFuncionario ";

Placed LEFT JOIN for it seems that not always you have the employee.

  • You have an error in your SQL syntax; check the manual that Corresponds to your Mysql server version for the right syntax to use near 'INNER JOIN client as cl on c.idCliente = cl.idCliente

  • @Andrébaill Sorry, I forgot a comma before the INNER take a look

  • Yes, SELECT worked. I was able to display the names correctly. Thanks for the help... @Maicon-Carraro

  • @Andrébaill If you have solved your problem, please mark the answer as the solution. :)

Browser other questions tagged

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