Left John returning more records

Asked

Viewed 1,063 times

1

I have a main table with about 5,000 records, and I need to search for information in another table with 7,000 records.

But my query is returning +6,000:

SELECT principal.id, info.nome, info.endereco, principal.valor
FROM principal
LEFT JOIN info
   ON info.usuario = principal.usuario

What can be happening?

2 answers

3


The LEFT JOIN, by definition, brings the rows of the table even if they are null.

What is likely to be happening in your case is that not all rows in the table principal have corresponding lines in the table info. Also, some table records principal must have more than one row in the table info.

1

There may be no records in the table info for all table records principal, but still this 5,000 records will show up, because the left join is actually a left outer join.

There is no left inner join or right inner join, for inner join is always bidirectional, ie there should be correspondence in the two tables for the join condition.

The thousand line difference is probably because a portion of the table records principal has two or more corresponding records in the table info.

Browser other questions tagged

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