bri bri - 19 days ago 5
MySQL Question

Get all rows from three different tables which contain same id

I have four tables.

**Users table** **Books Table** **Dvds Table** Toys Table
use_id boo_id dvd_id toy_id
use_name boo_name dvd_name toy_name
boo_use_id dvd_use_id toy_use_id


I need to extract all rows from the three tables; books, dvds and toys which contain the use_id. All i need from each of those rows is the unique ID of the book/dvd/toy.

My SQL is a little rusty but i think i need some kind of join.

Can anyone give me some direction please on my query?

Answer

Just join them :

SELECT u.*,
        b.boo_id,
        d.dvd_id,
        t.toy_id
FROM Users u
JOIN Books b
 ON(u.use_id = b.boo_use_id)
JOIN DVDs d
 ON(u.use_id = d.dvd_use_id)
JOIN Toys t
 ON(u.use_id = t.toy_use_id)
Comments