Problem with LEFT JOIN in PHP

Asked

Viewed 69 times

0

Hello, my friends.

I have 3 tables in my mysqli bank to record access to a particular location:

  1. visitor, whose data will always be filled in.

  2. corporate, whose data will be filled in only if the visitor represents a company.

  3. vehicle, whose details will be filled out if the visitor comes by car.

When making a search to return all results, I use the following query:

SELECT * 
FROM `visitante` 
LEFT JOIN `corporativo` ON `corporativo`.`id_visitante` = `visitante`.`id_visitante` 
LEFT JOIN `veiculo` ON `veiculo`.`id_visitante` = `visitante`.`id_visitante` 
WHERE `visitante`.`entrada` LIKE '%$data%' OR `visitante`.`status` LIKE '%Aberto%' 
GROUP BY `visitante`.`id_visitante` 
ORDER BY `visitante`.`entrada`;

What happens is that in the logs where I have no corporate visitor data or vehicle data, it returns my visitor id as NULL.

I think it’s some PHP problem because when doing this query in phpmyadmin, it returns all the data normally.

Does anyone have any suggestions???

Thank you.

  • From the format of your query it appears that you are not using [Prepared Statements] (https://secure.php.net/manual/en/mysqli.quickstart.prepared-statements.php). Maybe it’s some value in $data that is causing the problem?

  • @I really don’t know about these preparations. However I had a problem with this query in the system I did at hand and now I’m learning a little bit of Codeigniter and I’m rebuilding it in CI. I believe that the functions of the IC already incorporate these procedures. However, I removed all conditions and only left the left Join for the other tables and even then when I do not have the records of these tables, my id returns null. When other tables are filled in, id comes normal.

  • If I do the same query by giving a select visitor.id_visitor, all results come normal. Only when I give a select * do I have the problem.

  • There are 3 different id_visitors, the ones from the non-responsive table come null even. are you sure you’re looking at the right one? It would be good to remove that * from the query and use the fields correctly in the SELECT table.field or SELECT alias format., ....

  • There are many fields. But really the problem was the name. I simply changed the name in the database to visitnte_id and it returned me correctly. I do not know why the query does not return me what I want, even saying I was visitor.id_visitor. Thanks.

1 answer

0


Try creating aliases for the id column in the visiting table, or change the name. If in the tables involved there are several columns with the same name (id_visitor) it may be 'choosing' the last ones (corporate.id_visitane, vehicle.id_visitor) that in fact are empty and end up overwriting the value of the first one (visitor.id_visitor).

Remember that you do not need to bring three times the value of the id_visitor

The ideal would be something like this: (Just one example)

SELECT 
    visitante.id as id_visitante
    , visitante.nome
    , corpoartivo.empresa
    , veiculo.placa
FROM
    visitante 
    LEFT JOIN corporativo ON corporativo.id_visitante = visitante.id
    LEFT JOIN veiculo ON veiculo.id_visitante = visitante.id
  • It really was the name that was giving conflict. I do not know why the query does not return the correct id, even saying that I want from the visitor table. I changed the name of id as foreign key in the other tables and returned me correctly now. Mto thanks.

  • Arrange. I ask you to mark the question as accepted, to make it easier for other users who are experiencing similar problems.

Browser other questions tagged

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