1

现在我有三个mysql表。people我必须在一张桌子上列出所有的人。在此旁边它显示有兴趣,这是它的 sql。

SELECT *,(
           SELECT GROUP_CONCAT(interest_id SEPARATOR ",")
           FROM cat_people_interests
           WHERE person_id = people.id
         ) AS cat_interests
FROM people
WHERE id IN 
(
    SELECT person_id
    FROM cat_interests
)
ORDER BY lastname, firstname);

架构

`people` { int:id, varchar:name }
`cat_people_interests` { int:interest_id, int:person_id }
`cat_interests` { int:id, varchar:name }
`alternate_meow_pull` { int:person_id, int:meow_id, int:dataetc, int:dataetc2 }

所以这让我有能力在那里提取名字和所有兴趣。

如果一个人的 id 作为 person_id 在另一个表 (alternate_meow_pull) 中存在,我如何更改或添加到此以检查每行?我个人会做的只是检查每一行的 mysql 查询,但我确信有一种更快、更简洁的方法。顺便说一句,无论这个人是否存在于alternate_meow_pull 中,我都想得到结果。

因此,当我遍历这个结果时,我希望能够打印姓名、id、与人相关的所有兴趣,以及恰好在alternate_meow_pull 中与人相关的任何数据。

4

2 回答 2

1

不应该是这样吗?

SELECT  a.name, 
        GROUP_CONCAT(b.name) InterestList
FROM    people  a
        INNER JOIN cat_people_interest c
            ON a.ID = c.person_ID
        INNER JOIN cat_interests b
            ON b.id = c.interest_ID 
        INNER JOIN alternate_meow_pull d
            ON a.person_ID = d.person_id
WHERE   b.interest_id = '$_POST['showinterest_id']'
GROUP BY a.name

ID只会显示他们所在的人的姓名alternate_meow_pull

于 2012-09-19T07:29:14.973 回答
1
 SELECT people.id, people.name
 FROM people,cat_people_interests as c,cat_interests as i, alternate_meow_pull as m
 WHERE people.id = c.person_id or people.id = i.id or people.id = m.person_id
于 2012-09-19T07:39:23.760 回答