简体   繁体   English

mysql根据另一个表的计数选择行

[英]mysql select row based on count of another table

I have a table of blog posts in one table. 我在一个表中有一个博客帖子表。 In a separate table i have records of images that are stored in a directory that are tied to specific blog posts. 在一个单独的表中,我有图像记录,这些图像存储在与特定博客文章相关的目录中。

How can i write a query that selects only blog posts that have at least 5 photos? 我该如何编写查询以仅选择至少包含5张照片的博客文章?

The BlogPosts table has an 'ID' column. BlogPosts表具有“ ID”列。 The Photos table has a 'PostID' column that corresponds to the the BlogPosts' 'ID' column. Photos表的“ PostID”列与BlogPosts的“ ID”列相对应。

$query = "SELECT * FROM BlogPosts WHERE ?"
SELECT * 
FROM BlogPosts 
WHERE ID IN 
    (SELECT PostID FROM photos GROUP BY PostID HAVING count(*) >= 5)

Here's an example of a statement that should return the specified resultset. 这是一个应返回指定结果集的语句示例。

SELECT b.*
  FROM BlogPosts b
  JOIN (SELECT p.PostID
          FROM photos p
         GROUP BY p.PostID
        HAVING COUNT(1) >= 5
       ) c
    ON c.PostID = b.ID

This query makes use of an inline view, aliased as c . 此查询使用别名为c的内联视图。 The inline view is returning a distinct list of PostID values from the photos table for which there are at least five rows in the photos table. 内联视图从photos表返回一个不同的PostID值列表,在photo表中至少有五行。 Then it "joins" that resultset to the BlogPosts table, so it only returns rows that have an ID that matches a PostID in the subquery. 然后,它将结果集“联接”到BlogPosts表,因此它仅返回ID与子查询中的PostID匹配的行。

NOTE: On MySQL, this form, using the inline view, will (usually) perform better than using an IN (subquery) predicate. 注意:在MySQL上,使用内联视图的这种形式(通常)比使用IN (subquery)谓词的性能更好。

NOTE: Please quell any urge you have to "hide" this in a view, just for the sake of anyone else that later needs to read this code later, and figure out what the query is doing. 注意:请平息所有在视图中“隐藏”此内容的敦促,仅是为了其他人以后需要稍后阅读此代码,并弄清楚查询在做什么。

To get the rows in Photos where there are at least five per PostID... 要获取照片中的行,其中每个PostID至少有五行...

select PostID, count(PostID) as occurances from Photos group by PostID 从PostID的“照片”组中选择PostID,count(PostID)作为出现次数

To get only those where there are at least five... 只得到至少五个的那些...

select PostID from (select PostID, count(PostID) as occurances from Photos group by PostID) p where occurances >= 5 从中选择PostID(从PostID从“照片”组中选择PostID,count(PostID)作为出现次数)p其中出现次数> = 5

To get the blog posts that correspond... 要获取对应的博客文章...

select * from BlogPosts where ID in (select PostID from (select PostID, count(PostID) as occurances from Photos group by PostID) p where occurances >= 5); 从BlogPosts中选择*,其中ID位于(从中选择PostID,(从PostID中选择PostID,count(PostID)作为来自Photos组的事件),其中出现次数> = 5);

Or, really - you should first create a view that only returns the PostIDs from Photos that have more than five occurances: 或者,实际上-您应该首先创建一个视图,该视图仅返回出现次数超过5次的照片中的PostID:

create view atLeastFive as select PostID from (select PostID, count(PostID) as occurances from Photos group by PostID) p where occurances >= 5 在LeastFive创建视图,从中选择PostID(从PostID的Photos组中选择PostID,count(PostID)作为出现次数)p其中出现次数> = 5

Then your query looks a little more presentable: 然后,您的查询看起来更像样了:

select * from BlogPosts where ID in (select PostID from atLeastFive) 从ID中的BlogPosts中选择*(从atLeastFive中选择PostID)

Make sure that your Photos table has an index on PostID or the performance will end up suffering. 确保您的“照片”表在PostID上具有索引,否则性能最终会受到影响。

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

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