繁体   English   中英

检查每行的 id 是否作为另一个表中的 id 存在

[英]check per row if the id of a row exists as an id in another table

现在我有三个 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、与人相关的所有兴趣以及恰好在与人相关的 alter_meow_pull 中的任何数据。

不应该是这样吗?

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

只有在alternate_meow_pull上出现他们的ID的人的名字才会被显示。

 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

暂无
暂无

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

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