SQL Select inside a select with condition

0

I have 3 tables in the database to control a "news portal", where the tables have the following functions:

  • news - > News Table
  • gallery - > Table with images used in news
  • relationship - > Table relating news stories to images

Each news item can have more than one image, so the segmentation. I was able to work out a way to select the news and images belonging to it, however with the use of foreach, thus executing 2 SELECT, for example:

$noticia = "SELECT * FROM noticia";

foreach ($noticia as $row) {
    $id = $row['id'];

    $galeria = "SELECT a.* FROM galeria a, relacao b WHERE b.idNoticia = '$id' AND b.id_galeria = a.id";;
}

There are cases where the news may not have a picture yet.

I would like to know if you have any method to improve this code and perform the entire query in just one select, is it possible?

There are other areas that I can expand this same logic, for example, select the tags in the news (it follows the same logic, just changing the reference tables), but solving this problem, I can solve the rest.

    
asked by anonymous 19.12.2016 / 19:20

0 answers