1

我的网站上有一个特色部分,其中包含三种类型的特色帖子:普通、大和小。目前我正在三个单独的查询中获取这三种类型,如下所示:

@featured_big_first = Post.visible.where(pinged: 1).where('overlay_type =?', :big).limit(1)
@featured_big_first = Post.visible.where(pinged: 1).where('overlay_type =?', :small).limit(1)
@featured_big_first = Post.visible.where(pinged: 1).where('overlay_type =?', :normal).limit(5)

基本上,我正在寻找一个将这三者合二为一并获取 1 个大、1 个小、5 个普通帖子的查询。

4

2 回答 2

2

如果您不想要订单,我会感到惊讶。正如你所拥有的,它应该找到一个随机小、随机大和 5 个随机正态。

是的,您可以使用 UNION。但是,您必须执行 SQL。查看三个查询中每一个的 SQL 日志,并执行一个字符串的执行 SQL,该字符串是三个查询中的每一个,中间有 UNION。它可能有效,或者限制可能有问题。

在 SQL 中可以通过将表连接到自身,对表的一个别名进行分组,当另一个别名表 <= 按表分组时,添加一个有子句 where count of the < = 表低于限制。

因此,如果您对 posts 表有一个简单的查询(没有可见和 ping 条件)并且想要最新 created_at 日期的记录,那么正常查询将是:

SELECT posts1.*
FROM posts posts1, posts posts2
WHERE posts2.created_at >= posts1.create_at
  AND posts1.overlay_type = 'normal'
  AND posts2.overlay_type = 'normal'
GROUP BY posts1.id
HAVING count(posts2.id) <= 5

使用这条 SQL,并为可见和 pinged 添加条件,记住对帖子 1 和帖子 2 都使用条件。

然后把大版本和小版本写在一起,UNION一下。

我会坚持使用三个数据库调用。

于 2012-09-21T13:17:03.147 回答
1

我认为这是不可能的,但您可以使用scope哪种更方便的方式来编写代码

此外,它可能只是拼写错误,但您正在重新分配,@featured_big_first因此它将仅包含最后一个查询的数据

在 post.rb 中

  scope :overlay_type_record lamda{|type| joins(:visible).where(["visible.pinged=1 AND visible.overlay_type =", type])}

并在控制器中

@featured_big_first = Post.overlay_type_record(:big).limit(1)
@featured_small_first = Post.overlay_type_record(:small).limit(1)
@featured_normal_first = Post.overlay_type_record(:normal).limit(5)
于 2012-09-20T08:40:03.773 回答