Inner with 3 tables PHP / SQL

2

I have 3 tables and I want to compare the value of them, I can do with 2 using JOIN , but when I put JOIN on the third

asked by anonymous 05.09.2018 / 17:04

2 answers

2

Your error is in the second equal, you put "ud" and not "du"

$agora = "select * from usuario u 
          inner join dia_usuario du 
          on u.usuario_id = du.id_diarista 
          inner join diarista d 
          on d.id = du.id_diarista";
$sim = mysqli_query($conn,$agora);
while ($row_usuario2 = mysqli_fetch_assoc($sim)) {
    $ola2 = $row_usuario2['nome'];
}

=========

You only find the enerson user because of the following ...

You are picking up all the user data from different tables where the id of the user is equal to the id of the day and the id of the daily table (Not the id_dialist) is equal to the id_dialist ...

So the user enerson has id 3 which is equal to id_dialist 3 which is equal to id of the daily table which is also 3 ... The other value is 5 and there is no user with id 5 at the moment, Did you get it out?

    
05.09.2018 / 17:25
0

What is happening is that you are restricted in your search for the contents of related tables.

What does this mean?

Your join restricts the returns from the final set by filtering the results that are in the sets that are related to the primary set.

If you want to bring the data that is present in the main table and coalesce from what does not exist in tables B and C of your search, you should use the left join.

In this case, your query would look like this:

select 
* 
from usuario u 
left join dia_usuario du 
  on u.usuario_id = du.id 
left join diarista d 
  on d.id = du.id;

I do not want to look rude or pretentious, but I see that you seem to be starting in the database studies, I could see that your modeling still does not reflect well the reality of what you want with your application.

I suggest to study a little more how the joins work in the queries, for this follows a small but interesting article below:

link

Good luck there in your application, and I hope my contribution is helpful.

    
05.09.2018 / 20:54