Is there a way to join multiple SELECTS in just one query?

0

I have a page where I will have to make several queries to the bank. At present, they are a total of three tables. They are: cadastro , publicacoes and estilo . Currently I do this:

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 foreach... .

My question is: Is there any way to merge all of these querys into a single query, thus leaving the code cleaner and more performative?

    
asked by anonymous 31.01.2016 / 00:28

1 answer

1

I think this query solves your problem, you can join them using inner join because it has the user reference (whereas user is unique)

     SELECT cadastro.nome, cadastro.sobrenome, cadastro.usuario, cadastro.email,publicacoes.conteudo, publicacoes.data, publicacoes.hora, estilo.cor1,estilo.cor2, estilo.cor3 
     FROM publicacoes INNER JOIN cadastro ON cadastro.usuario =publicacoes.usuario 
     INNER JOIN estilo ON estilo.usuario = publicacoes.usuario 
     WHERE usuario = "$usuario" limit 5
    
31.01.2016 / 00:53