问题
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