Yep like this
SELECT
a.id,
b.*
FROM
tableB AS b
JOIN
tableA as a ON a.reference_id = b.id
WHERE
b.id = :id
The :id
is a named place holder for PDO
which I highly recommend using.
$stmt = $PDO->prepare($sql);
$stmt->execute([':id'=>2]);
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
Now at first glance you might think this is like @Karlo Kokkak answer. Well I'm using Join
not Inner Join
. Just kidding, I know they are the same thing. I just wanted a better way to say it then "they are the same thing".
The real difference is that I actually used the reference_id
as the foreign key field. And I arranged the tables with the One relation first and then the Many relation second.
tableB has a One to Many relationship with tableA, because tableA has the FK in it. Therefor you can have many records in tableA that refer to tableB. Assuming reference_id
is the FK, but I don't see what else it could be. I would consider it a horrible design if the id
in one table was the Primary key, and then the id
in the other table was the FK.
Anyway, My table arrangement is backwards to that one, but IMO the correct way (in most cases). This is probably an oversite by the OP.
Last thing is I used a prepared statement in mine.
Cheers.