简体   繁体   中英

Comparing an id to id of different tables rows mysql

So I am trying to retrieve all interests from someone, and be able to list them. This works with the following query.

SELECT *,(
    SELECT GROUP_CONCAT(interest_id SEPARATOR ",")
    FROM people_interests
    WHERE person_id = people.id
) AS interests
FROM people
WHERE id IN (
    SELECT person_id
    FROM people_interests
    WHERE interest_id = '.$site->db->clean($_POST['showinterest_id']).'
)
ORDER BY lastname, firstname

In this one which I am having trouble with, I want to select only those who happen to have their id in the table named volleyballplayers . The table just has an id, person_id, team_id, and date fields.

SELECT *,(
    SELECT GROUP_CONCAT(interest_id SEPARATOR ",")
    FROM people_interests
    WHERE person_id = people.id
) AS interests
FROM people
WHERE id IN (
    SELECT person_id
    FROM people_interests
    WHERE volleyballplayers.person_id = person_id
)
ORDER BY lastname, firstname

I just want to make sure that only the people who are in the volleyballplayers table show up, but I am getting an error saying that Unknown column 'volleyballplayers.person_id' in 'where clause' although I am quite sure of the name of table and I know the column is named person_id.

Try to join it with a subquery,

SELECT  *, GROUP_CONCAT(interest_id) interests
FROM    people a
        INNER JOIN people_interests b
            ON b.person_id = a.id
        INNER JOIN
        (
            SELECT DISTINCT person_id
            FROM volleyballplayers
        ) c ON b.person_id = c.person_id
GROUP BY a.id
ORDER BY lastname, firstname

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