简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM