How to get the results of two tables by consulting one with PDO?

Asked

Viewed 589 times

1

I have the establishment table, unit table and professional table. In the unit table, I have the idestablishment. On the professional table, I have the skill. I need to pull all the professionals' ID from the establishment. Another thing, the establishment has N units and each unit has N professionals.

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

PHP:

<?php
$idestabel = $_GET['idestabel'];

$getUnits=$pdo->prepare("SELECT idunidade, unidade FROM unidade WHERE idestabelecimento=:idestabel");
$getUnits->bindValue(":idestabel", $idestabel);
?>

1 answer

2


It seems to be just a matter of performing a query with Inner Join in a many relationship for 1, follow 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 relationships for many

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 a parent of professionals, being a relationship of 1 unit for many professionals (according to the structure presented), applying the example of 1 for many or many to 1, you will get the expected result.

  • 1

    I tried your 1st. suggestion and it worked. Thanks.

Browser other questions tagged

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