6

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
RooSoft
  • 1,359
  • 2
  • 15
  • 27

2 Answers2

7
DB[:albums___a].
  right_join(:tracks___t, :album_id=>:id).
  select_group(:a__id).
  select_more{count(:t__id)}
Jeremy Evans
  • 11,361
  • 23
  • 25
0

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.

zebediah49
  • 7,177
  • 1
  • 28
  • 48