现在我有三个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);
SCHEMA
`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在另一个表(alternate_meow_pull)中作为person_id存在,我如何更改或添加到每行来检查?我个人所做的只是检查每一行的mysql查询,但我确信有更快更简洁的方法。我想得到结果,无论这个人是否存在于alternate_meow_pull中。
因此,当我遍历此结果时,我希望能够打印名称,ID,与人相关的所有兴趣,以及恰好位于与人相关的alternate_meow_pull中的任何数据。
答案 0 :(得分: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
出现的人的姓名。
答案 1 :(得分: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