检查多行中是否存在一条记录

时间:2010-08-02 12:08:21

标签: mysql

我有两个表,其结构如下:

`content` (id, foreign_id, type, date_added)
`content_territory` (id, content_id, territory_iso)

这些表的结构旨在为每个有权访问该内容的区域提供一条content_territory条记录。这意味着一个content项目可以拥有超过200个领土记录。

我遇到的问题是,在列出最新内容时,我想验证内容是否具有区域IN()区域 - 并且如果没有显示则忽略该记录。

3 个答案:

答案 0 :(得分:0)

select id, foreign_id, type, date_added
from content
where id in (select content_id from content_territory)

答案 1 :(得分:0)

   SELECT c.*
     FROM content AS c
LEFT JOIN content_territory AS ct
       ON c.id = ct.content_id
    WHERE ct.id IN ( ... )
 GROUP BY c.id

答案 2 :(得分:0)

SELECT id, foreign_id, type, date_added
    FROM content
    WHERE EXISTS (SELECT *
                      FROM content_territory
                      WHERE content_id = content.id);