Reputation: 286
I want to count all items with in Clause
like i have a following Query
SELECT `vote` from `tablename` where `vote_id` in (1,2,3,4);
I dont have an idea how to count items for each record in above query .
Can anybody tell me how to do that.
Upvotes: 1
Views: 78
Reputation: 1320
select count(vote_id)
from `tablename`
where `vote_id`in (1,2,3,4);
Upvotes: 2
Reputation: 242
SELECT vote_id,count(vote) from tablename where vote_id in (1,2,3,4) GROUP BY vote_id
Upvotes: 2
Reputation: 4275
Use count()
function in sql to count the no. of rows
SELECT COUNT(`vote`) as votesno from `tablename` where `vote_id` in (1,2,3,4) GROUP BY votesno;
Upvotes: 3
Reputation: 2802
for total counts
SELECT count(`vote`) from `tablename` where `vote_id` in (1,2,3,4);
and for individual count
SELECT `vote_id`,count(`vote`) from `tablename` where `vote_id` in (1,2,3,4) group by `vote_id`;
Upvotes: 2
Reputation: 15783
If you want to count the rows for each id you have in your IN
clause just count the records grouping by vote_id
:
SELECT COUNT(`vote`) as votes
FROM `tablename`
WHERE `vote_id` IN (1,2,3,4)
GROUP BY `vote_id`;
Upvotes: 2