Hitu
Hitu

Reputation: 286

Count Records with in Clause , may add where condition

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

Answers (5)

Nitu Dhaka
Nitu Dhaka

Reputation: 1320

select count(vote_id)
from `tablename`
 where `vote_id`in (1,2,3,4);

Upvotes: 2

Edrich
Edrich

Reputation: 242

SELECT vote_id,count(vote) from tablename where vote_id in (1,2,3,4) GROUP BY vote_id

Upvotes: 2

Utkarsh Dixit
Utkarsh Dixit

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

senK
senK

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

Ende Neu
Ende Neu

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

Related Questions