Vera Perrone
Vera Perrone

Reputation: 369

Many-to-Many mySQL selection

How can I get a select result showing all the interests of a certain user. My 3 tabels look like this:

Tabel: User
-- ID
-- first_name
-- last_name
-- etc...

Tabel: Interests
-- ID
-- title

Tabel: User_Interests
-- User_ID
-- Interests_ID

This is what I got so far:

SELECT 
    User.ID, User.first_name
FROM
    User
        INNER JOIN
    User_Interests ON User_Interests.User_ID = User.ID
WHERE
    User.ID = 0

    enter code here

Upvotes: 1

Views: 31

Answers (1)

Oto Shavadze
Oto Shavadze

Reputation: 42753

You need just also join to Interests table

Try this:

SELECT 
User.ID, User.first_name, Interests.title 
FROM  User
INNER JOIN User_Interests 
ON User_Interests.User_ID = User.ID
INNER JOIN Interests 
ON User_Interests.Interests_ID = Interests.ID 
WHERE
User.ID = 0

Upvotes: 2

Related Questions