加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – 使用续集gem的多个聚合查询

发布时间:2020-12-17 01:37:18 所属栏目:百科 来源:网络整理
导读:是否可以使用续集来执行这样的查询: select (select count(*) from users where blah = 'blah') as "users",(select count(*) from contacts where blah = 'blah') as "contacts" 我知道我可以使用续集一次执行这些查询,但我想同时执行它们. 解决方法 您可
是否可以使用续集来执行这样的查询:

select (select count(*) from users where blah = 'blah') as "users",(select count(*) from contacts where blah = 'blah') as "contacts"

我知道我可以使用续集一次执行这些查询,但我想同时执行它们.

解决方法

您可以在不使用以下内容编写原始SQL的情况下执行该查询:

dataset = DB.select {[ 
  DB[:users].where(blah: 'blah').select { count('*') }.as(:users),DB[:contacts].where(blah: 'blah').select { count('*') }.as(:contacts) 
]}

dataset.first
# => { users: X,contacts: Y }

dataset.sql
# => "SELECT (SELECT count('*') FROM "users" WHERE ("blah" = 'blah')) AS "users",#            (SELECT count('*') FROM "contacts" WHERE ("blah" = 'blah')) AS "contacts""

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读