Multitasking queries in MySQL

1

I am doing a query with MySQL where I have some establishments and in another table I have the comments of the establishment.

This is the query I do:

SELECT *
FROM establecimientos
INNER JOIN comentarios ON establecimientos.id = comentarios.id_establecimiento
WHERE establecimientos.id = 30

It shows it to me but when the table comentarios has a comentarios.id_establecimiento equal to establecimientos.id . What I'm looking for is that although there are no comments the same shows me the data of the query.

    
asked by Avancini1 02.04.2018 в 06:28
source

2 answers

3

You must use LEFT JOIN to be able to show even value nulls

The first thing you should do is replace Inner Join with Left join since the latter will bring you all the results even if you do not find matching values

  

The difference is that INNER JOIN does look for matches in both tables; that is, all the data in the left table that has a record associated with the table on the right

SELECT * FROM establecimientos 
LEFT JOIN comentarios 
ON establecimientos.id = comentarios.id_establecimiento 
WHERE establecimientos.id = 30;

What are the differences between INNER JOIN Y LEFT JOIN ?

  • Inner join of beta tables and range (they are name of mere example) will return as a result all matches or intersections of both tables
  • Left join Left-hand combinations include all records in the first left table, even if there are no matching records for the records in the second right table.
  • answered by 02.04.2018 / 06:34
    source
    2

    What you are looking for is to make a LEFT JOIN :

    SELECT *
    FROM establecimientos
    LEFT JOIN comentarios ON establecimientos.id = comentarios.id_establecimiento
    WHERE establecimientos.id = 30
    

    In this way, even if there are no comments, the query will bring you the data of establecimiento and null data in comentarios .

        
    answered by 02.04.2018 в 06:34