6

简而言之,如何使用Sequel进行此查询?

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

2 回答 2

8
DB[:albums___a].
  right_join(:tracks___t, :album_id=>:id).
  select_group(:a__id).
  select_more{count(:t__id)}
于 2012-06-04T19:18:37.260 回答
0

您的问题是左连接为每个专辑 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而不是加入。

于 2012-06-04T16:47:45.710 回答