Equal Employee Return

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;

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

What would have gone wrong there?

I thought of trying to do this SQL, it only displays those that are set the employees on the call, but there are calls in "open" with 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

    
asked by anonymous 10.06.2015 / 01:10

1 answer

2

If you want to add the name of the employee in the return of the query, it is not easier to find it in SELECT ?

I think your idFuncionario is in the Chamada table, 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 ";

I put LEFT JOIN because it seems that you do not always have the employee.

    
10.06.2015 / 01:33