简体   繁体   English

将ID与不同表行的ID进行比较mysql

[英]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 . 在我遇到的麻烦中,我只想选择那些恰好在表volleyballplayers拥有其ID的用户。 The table just has an id, person_id, team_id, and date fields. 该表仅具有一个id,person_id,team_id和日期字段。

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. 我只是想确保只显示排球运动员表中的人,但是我得到一个错误,说Unknown column 'volleyballplayers.person_id' in 'where clause'中的Unknown column 'volleyballplayers.person_id' in 'where clause'尽管我非常确定表的名称和我知道该列名为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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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