简而言之,如何使用Sequel进行此查询?
select a.id, count(t.id)
from albums a
right join tracks t on t.album_id = a.id
group by a.id
DB[:albums___a].
right_join(:tracks___t, :album_id=>:id).
select_group(:a__id).
select_more{count(:t__id)}
您的问题是左连接为每个专辑 ID 找到一个轨道 ID。解决方案:
right join
总和的子查询,假设续集支持:left join (select album_id, count(album_id) as count from tracks group by album_id) t on
海峡向上from albums a, tracks t where t.album_id=a.id
而不是加入。