How to display results if my select turns out to be false?

Asked

Viewed 36 times

0

And I’m having the following problem, there are two POSTS and TOPIC tables, one topic has several posts, and I’m displaying the topics by sorting the most current date of the topic post, only when creating a new topic it doesn’t display on the screen because it has no post, and if I post on the topic it displays on the screen because it has post, and I wanted to know how I can display the topics that have no posts? How can I list the topics that have no posts? Because my JOINN is only bringing the topics that have related posts

$sqlTopicos = mysqli_query($conexao, 
        "SELECT topico.titulo, posts.idTopico, slug, posts.dataPost, usuario.usuario, visualizacoes, qtdPosts 
         FROM (SELECT * FROM posts ORDER BY dataPost DESC) posts 
         INNER JOIN topico 
            ON posts.idTopico = topico.idTopico
         INNER JOIN usuario
            ON topico.idUsuario = usuario.idUsuario
         WHERE posts.idArea = '$idArea' GROUP BY titulo ORDER BY posts.dataPost DESC");

1 answer

0

Make a Left Join between the tables: http://www.w3schools.com/sql/sql_join_left.asp. It will bring the results of Topics even if there are no associated Posts.

Example:

SELECT * FROM Topicos t
LEFT JOIN Posts p
ON t.TopicoID=p.TopicoID
ORDER BY t.DataCriacao;
  • I got it this way:

  • Did not appear its solution, put again.

Browser other questions tagged

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