More than one table in the query - LEFT JOIN SQL

Asked

Viewed 421 times

1

Help with SQL:

Query:

SELECT * FROM concursos 
LEFT JOIN concursos_categorias ON concursos.id = concursos_categorias.idConcurso 
LEFT JOIN categorias_concursos ON concursos_categorias.idCategoria = categorias_concursos.id 
WHERE concursos.id = 15
  • concursos is my table where I have all my contests
  • categorias_concursos is the table where all categories
  • concursos_categorias is my table where the contest categories

on the table concursos_categorias has the columns idConcurso and idCategoria

The query she’s bringing me the contest categories, but I also needed to bring the other categories, not repeating the ones I have from this contest.

  • 2

    If you discovered the solution, post it as a response and mark it as an accepted response. In addition valid is encouraged.

2 answers

2

SOLUTION

SELECT * FROM categorias_concursos cat 
LEFT JOIN concursos_categorias con ON cat.id = con.idCategoria and con.idConcurso = '$id' 
LEFT JOIN concursos c ON con.idConcurso = c.id

0

Try using group_by that so it elects the repeated columns.

Browser other questions tagged

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