0
I have a problem with my code SQL. Well, I have this function that is in the class Usuario
:
public function CS_GetUsuarios($sql_aux = false)
{
global $objConexao;
$strSql = "SELECT U.*, A.*, G.*,
(
SELECT COALESCE(SUM(V.countView), 0) FROM usuario_visitas V WHERE V.codUsuario = U.codUsuario
) as total_visitas,
(
SELECT COALESCE(SUM(F.countVotos), 0) FROM usuario_votos F WHERE F.codUsuario = U.codUsuario
) as total_votos
FROM usuario U
LEFT JOIN usuario_visitas A ON U.codUsuario = A.codUsuario
LEFT JOIN usuario_fotos G ON G.cod_usuario = U.codUsuario
WHERE U.indAtivo = 'S' AND G.indExibir = 'S'
$sql_aux";
$vetDados = $objConexao->query($strSql)->fetchAll();
return $vetDados;
}
And on the index I’m calling the same:
$vet_dados_top = Usuario::CS_GetUsuarios("AND U.tipo_conta = '1' GROUP BY U.codUsuario ORDER BY total_visitas DESC LIMIT 12");
Well, my problem is that, it just returns me the data complete users who have visits (record) in the table usuario_visitas
. When the user has not received any visitors in the profile it specifically returns the codUsuario = null
, which, in my logic, I programmed for him to bring all the records, even those users who have no visit.
Giving a var_dump()
in $vet_dados_top
the result is more or less this:
User who has visit
array (size=121) 'codUsuario' => string '1182652231' (length=10) 0 => string '1182652231' (length=10) 'strLogin' => string 'fulano' (length=7) 1 => string 'fulano' (length=7) 'strSenha' => string 'XXXXXXXXXXXXX' (length=32) 2 => string 'XXXXXXXXXXXXX' (length=32) [...]
User who has no visits
array (size=121) 'codUsuario' => null 0 => string '1182652232' (length=10) 'strLogin' => string 'ciclano' (length=5) 1 => string 'ciclano' (length=5) 'strSenha' => string 'XXXXXXXXXXXX' (length=32) 2 => string 'XXXXXXXXXXXX' (length=32) [...]
The table usuario_visitas
does not have the codes of all users, only the ones that were visited. Visited, was added to the table.
Where I’m missing the thought?
Rovann, it didn’t work out. Same thing as Motta, keeps bringing the
codUsuario = null
user who has no visits.– Thiago
note if the column codUsuario is from table A or U, as both are with the same name
– Rovann Linhalis
I changed the part of Coalesce tb, despite not influencing the issue, but it makes a difference in the sum
– Rovann Linhalis
I believe you don’t need group by either, maybe a distinct
– Rovann Linhalis
It continued in the same, I made an issue in my original question. Where I quote that table
usuario_visitas
does not have all user codes, has some relationship with what is happening?– Thiago
Well, if I literally remove * from the table it doesn’t work. I did so
SELECT U.*, G.*,
worked as it should. But what was happening after all?– Thiago
table A also has a column of name codUsuario, and it will always come as null when there are no visits, I believe you were picking up the value of it
– Rovann Linhalis
I made one more change that might help you
– Rovann Linhalis
Got it. Thanks! You’re from my state, Colatina hehe, I’m from Vitória. Hug!
– Thiago
That’s it! Rsrs for nothing and hugs
– Rovann Linhalis