Norbert Pisz
Norbert Pisz

Reputation: 3440

Join other table to get count

I have a table contains items:

items: id, body, user_id

and second table constains votes:

items_votes: id, item_id, type

I getting all items by simple query:

SELECT * FROM items

How I can get votes count for every item in this query?

Upvotes: 0

Views: 66

Answers (2)

Vijay
Vijay

Reputation: 8451

try out this...

select item.id, item.type, item.user_id, count(*)
FROM items item inner join
items_votes iv on item.id = iv.item_id
group by item.id

SQL Fiddle

Upvotes: 1

Arun Kumar M
Arun Kumar M

Reputation: 848

select i.id, i.type, i.user_id, count(*)
FROM items i,items_votes v WHERE i.id = v.item_id
group by i.id

SQLFIDDLE

Upvotes: 1

Related Questions