Reputation: 17185
I am working on functionality that lets users vote on particular items. I made an item table and a votes table. The votes table has vote column that will either have 1 or -1
When getting items, I am assuming that I will just do a join on items and votes, and sum up the votes column.
Is that a scalable approach? So far I have this query:
select item_id , title , description , votes from items
left join votes on
items.item_id = votes.item_id
order by item_id desc
But how do I use the sum function on the votes column in the same query?
Thanks!!
Upvotes: 1
Views: 289
Reputation: 43265
select item_id , title , description , sum(votes) as totalVotes from items
left join votes on
items.item_id = votes.item_id
group by votes.item_id
order by item_id desc
Upvotes: 5