在 Rails 中查询属性组合 - 如何将 9 个查询变成 1 个?
有什么办法可以将这些查询压缩为一个查询吗?
我正在查询 1 个表的 4 个属性:author_id
、status
、category_id
和 site_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 代码中通过它们。这可能吗?这里最好的方法是什么?
更新
以下大致是我用来生成这些查询的代码:
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:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从 SQL 端,您可以使用 UNION 来组合具有匹配字段列表的查询。添加一个字段对它们进行排序,并将其全部包装在另一个查询中以按其排序。然后您可以迭代结果集。
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.