Continued: How to use the group and account

Simply put, how can I make this request 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
+5
source share
2 answers
DB[:albums___a].
  right_join(:tracks___t, :album_id=>:id).
  select_group(:a__id).
  select_more{count(:t__id)}
+5
source

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

right join

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

spill up from albums a, tracks t where t.album_id=a.idinstead of joining.

0
source

All Articles