Query using INNER JOIN with problem

Asked

Viewed 277 times

1

I’m making an appointment using INNER JOIN, but when I do the JOIN on the table A with the B and there is no id in the table B, he returns me a array empty. I would like it if it did not find the id in the table B return to table A either way or both but with the blank fields in the table B. I’m using the following code:

SELECT *
FROM projects AS p
INNER JOIN project_images AS img ON p.id = img.project_id
WHERE p.id = 44
LIMIT 1
  • Put the structure of the tables, please.

2 answers

3


Exchange the INNER JOIN for LEFT JOIN.

  • Thanks guy worked. Could you explain to me the difference between INNER AND LEFT?

  • 2

    @Leandrocosta View this question: http://answall.com/questions/6441/qual-e-a-diferenca-entre-inner-join-e-outer-join

  • @bfavaretto thanks man!

0

Complementing @Rodrigo Rigotti’s response

INNER JOIN returns records from two tables, for example A and B, when a record with ID in A is equal to a record with foreign ID in B. Example

... A INNER JOIN B ON A.ID = B.ID_A ...

will only return records that match in both tables.

LEFT JOIN returns ALL records from the table to the left of the test. So if:

... A LEFT JOIN B ON A.ID = B.ID_A ...

return all records of A, for those where B has ID_A matching, the fields of B will be filled with this record, for those where there is no ID_A matching, the fields of B will be with null value.

Browser other questions tagged

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