比较id到不同表行的id mysql

时间:2012-10-04 21:36:00

标签: php mysql

所以我试图从某人那里获取所有兴趣,并能够列出它们。这适用于以下查询。

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

在我遇到问题的那个中,我想只选择那些碰巧在名为volleyballplayers的表中拥有自己身份的人。该表只有一个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

我只是想确保只有排球运动员表中的人出现,但我收到错误说Unknown column 'volleyballplayers.person_id' in 'where clause'虽然我非常确定表的名称,但我知道列被命名为person_id。

1 个答案:

答案 0 :(得分:1)

尝试使用子查询

加入它
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