在 Rails 中查询属性组合 - 如何将 9 个查询变成 1 个?

发布于 2024-11-05 12:18:40 字数 1794 浏览 0 评论 0原文

有什么办法可以将这些查询压缩为一个查询吗?

我正在查询 1 个表的 4 个属性:author_idstatuscategory_idsite_id。目标是,我想找到每种情况下的最新记录(原因是增加报告计数器):

# author_id
SELECT * FROM posts WHERE posts.author_id = 10 ORDER BY created_at desc LIMIT 1

# author_id + status
SELECT * FROM posts WHERE posts.author_id = 10 AND posts.status = 'draft' ORDER BY created_at desc LIMIT 1
SELECT * FROM posts WHERE posts.author_id = 10 AND posts.status = 'published' ORDER BY created_at desc LIMIT 1

# author_id + site_id
SELECT * FROM posts WHERE posts.author_id = 10 AND posts.site_id = 1 ORDER BY created_at desc LIMIT 1

# author_id + status + site_id
SELECT * FROM posts WHERE posts.author_id = 10 AND posts.status = 'draft' AND posts.site_id = 1 ORDER BY created_at desc LIMIT 1
SELECT * FROM posts WHERE posts.author_id = 10 AND posts.status = 'published' AND posts.site_id = 1 ORDER BY created_at desc LIMIT 1

# author_id + site_id + category_id
SELECT * FROM posts WHERE posts.author_id = 10 AND posts.site_id = 1 AND posts.category_id = 7 ORDER BY created_at desc LIMIT 1

# author_id + status + site_id + category_id
SELECT * FROM posts WHERE posts.author_id = 10 AND posts.status = 'draft' AND posts.site_id = 1 AND posts.category_id = 7 ORDER BY created_at desc LIMIT 1
SELECT * FROM posts WHERE posts.author_id = 10 AND posts.status = 'published' AND posts.site_id = 1 AND posts.category_id = 7 ORDER BY created_at desc LIMIT 1

这是 9 个查询。我正在 Rails 中执行此操作,因此目标是通过一次 Post.all(:conditions => x) 调用获得 9 个 Post 模型,然后我可以过滤在 ruby​​ 代码中通过它们。这可能吗?这里最好的方法是什么?

更新

以下大致是我用来生成这些查询的代码:

https://gist .github.com/965233

Is there any way to condense these queries down to one query?

I am querying 1 table for 4 attributes: author_id, status, category_id, and site_id. The goal is, I want to find the most recent record in each case (reason is to increment counters for reporting):

# author_id
SELECT * FROM posts WHERE posts.author_id = 10 ORDER BY created_at desc LIMIT 1

# author_id + status
SELECT * FROM posts WHERE posts.author_id = 10 AND posts.status = 'draft' ORDER BY created_at desc LIMIT 1
SELECT * FROM posts WHERE posts.author_id = 10 AND posts.status = 'published' ORDER BY created_at desc LIMIT 1

# author_id + site_id
SELECT * FROM posts WHERE posts.author_id = 10 AND posts.site_id = 1 ORDER BY created_at desc LIMIT 1

# author_id + status + site_id
SELECT * FROM posts WHERE posts.author_id = 10 AND posts.status = 'draft' AND posts.site_id = 1 ORDER BY created_at desc LIMIT 1
SELECT * FROM posts WHERE posts.author_id = 10 AND posts.status = 'published' AND posts.site_id = 1 ORDER BY created_at desc LIMIT 1

# author_id + site_id + category_id
SELECT * FROM posts WHERE posts.author_id = 10 AND posts.site_id = 1 AND posts.category_id = 7 ORDER BY created_at desc LIMIT 1

# author_id + status + site_id + category_id
SELECT * FROM posts WHERE posts.author_id = 10 AND posts.status = 'draft' AND posts.site_id = 1 AND posts.category_id = 7 ORDER BY created_at desc LIMIT 1
SELECT * FROM posts WHERE posts.author_id = 10 AND posts.status = 'published' AND posts.site_id = 1 AND posts.category_id = 7 ORDER BY created_at desc LIMIT 1

That's 9 queries. I am doing this in rails, so the goal would be to have 9 Post models from one Post.all(:conditions => x) call, then I can filter through them in the ruby code. Is this possible? What's the best approach here?

Update

Here is roughly the code I'm using to generate these queries:

https://gist.github.com/965233

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白云悠悠 2024-11-12 12:18:40

从 SQL 端,您可以使用 UNION 来组合具有匹配字段列表的查询。添加一个字段对它们进行排序,并将其全部包装在另一个查询中以按其排序。然后您可以迭代结果集。

SELECT * FROM 
(    
  SELECT 1 QNO, * FROM SOMETABLE WHERE A=1
  UNION ALL
  SELECT 2 QNO, * FROM SOMETABLE WHERE A=1 AND B=2
  UNION ALL
  SELECT 3 QNO, * FROM SOMETABLE WHERE A=2 AND B=4 AND C=11
) 
ORDER BY QNO;

From the SQL side you can use UNION to combine queries that have a matching list of fields. Add a field to sort them and wrap it all in another query to sort by that. Then you can iterate over the result set.

SELECT * FROM 
(    
  SELECT 1 QNO, * FROM SOMETABLE WHERE A=1
  UNION ALL
  SELECT 2 QNO, * FROM SOMETABLE WHERE A=1 AND B=2
  UNION ALL
  SELECT 3 QNO, * FROM SOMETABLE WHERE A=2 AND B=4 AND C=11
) 
ORDER BY QNO;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文