How do I get the results of two tables by querying one with PDO?

1

I have the table establishment, table unit, and table professional. In the unit table, I have idestablishment. On the professional table, I have the idunity. I need to find all the data of the professionals by the id of the establishment. Another thing, the establishment has N units and each unit has N professionals.

PHP:

<?php$idestabel=$_GET['idestabel'];$getUnits=$pdo->prepare("SELECT idunidade, unidade FROM unidade WHERE idestabelecimento=:idestabel");
$getUnits->bindValue(":idestabel", $idestabel);
?>
    
asked by anonymous 09.08.2017 / 22:11

1 answer

2

It seems to be just a matter of conducting a query with Inner Join in a many-to-one relationship, an example for each situation (many for 1, 1 for many and many for many):

// for relationship 1 for many or many for 1

SELECT * FROM TabelaPai
INNER JOIN TabelaFilha ON TabelaPai.Id = TabelaFilha.IdTabelaPai
WHERE TabelaFilha.IdDeAlgumaCoisa = AlgumId

// for many-to-many relationships

SELECT * 
FROM Tabela1
INNER JOIN (
    Tabela2 INNER JOIN TabelaLigacao ON Tabela2.Id = TabelaLigacao.IdTabela2
) ON Tabela1.Id = TabelaLigacao.IdTabela1
WHERE Tabela2.IdDeAlgumaCoisa = AlgumId

Treat the unit table as the parent of professionals, being a relationship of 1 unit for many professionals (according to the structure presented), applying the example of 1 to many or many to 1 , you will get the expected result.

    
09.08.2017 / 22:25