0
I have a page where I will have to make several queries to the bank. At the moment, they are a total of three tables. They are: cadastro
, publicacoes
and estilo
. Currently I do as follows:
SELECT nome, sobrenome, usuario, email FROM cadastro WHERE usuario = "$usuario" LIMIT 1;
SELECT conteudo, data, hora, usuario FROM publicacoes WHERE usuario = "$usuario" ORDER BY id LIMIT 5;
SELECT cor1, cor2, cor3, usuario FROM estilo WHERE usuario = "$usuario" LIMIT 1;
All queries are made and then I manipulate each query
through the foreach...
.
My question is: Is there any way to join all these querys in a single query, thus leaving the code more "clean" and performative?
Yes it is possible, in which case you need to do Join, you can start here
– rray
I had already seen this question yesterday. But the question is: can you use Join even without relating the tables (which is my case)? Or I’m full of shit?
– Igor
If I understand correctly, the information they have in common is the field
usuario
he will be the joint criterio.– rray
SELECT * FROM (SELECT C.*, P.*, E.* FROM cadastro as C
LEFT JOIN publicacoes AS P ON C.usuario = P.usuario
LEFT JOIN estilo AS E ON C.usuario = E.usuario) RESULTADO WHERE usuario = '".$usuario."'
– StillBuggin