Sequel: How to use group and count

蓝咒 提交于 2019-12-21 13:04:25

问题


Simply put, how can I do this query using Sequel?

select a.id, count(t.id)
from albums a
right join tracks t on t.album_id = a.id
group by a.id

回答1:


DB[:albums___a].
  right_join(:tracks___t, :album_id=>:id).
  select_group(:a__id).
  select_more{count(:t__id)}



回答2:


Your problem is that the left join finds a track ID for each album ID. Solutions:

right join

subquery of sums, assuming sequel supports that: left join (select album_id, count(album_id) as count from tracks group by album_id) t on

a strait up from albums a, tracks t where t.album_id=a.id instead of the join.



来源:https://stackoverflow.com/questions/10884871/sequel-how-to-use-group-and-count

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!