Mysql 检查每行的id是否作为另一个表中的id存在
现在我有三个mysql表。我必须在表格中列出Mysql 检查每行的id是否作为另一个表中的id存在,mysql,sql,database,Mysql,Sql,Database,现在我有三个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
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是否作为另一个表中的人员id存在(备用)?我个人会做的只是在每一行上使用mysql查询进行检查,但我相信有一种更快更简洁的方法。我想得到结果,不管这个人是否存在,顺便说一句
所以,当我循环这个结果时,我希望能够打印出姓名、id、所有与person相关的兴趣,以及任何与person相关的数据。应该是这样吗
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\u meow\u pull
上显示的人员的姓名。您可以添加示例记录和所需输出吗?按顺序中的“firstname”不应该是“firstname”吗clause@Michael你说得对,修正了&谢谢。@JohnWoo我已经添加了更多关于它的信息,谢谢。您在这里也有错误“'showinterest\u id'”,它不应该是字符串的一部分吗?例如('.clean($\u POST[showinterest\u id])),我想您的意思是其中的interest\u id=showinterest\u id,(showinterest是从外部输入的某种类型的整数)?我如何更改它以使备用\u meow\u pull可选?有些人不会有任何兴趣爱好,有些人也不会参加备战,我仍然希望在那里画出一个名字,并为兴趣爱好和备战兴趣爱好提供空结果?@jett使用左连接
而不是内部连接
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