shitanshu
shitanshu

Reputation: 222

Mysql Query give different counts first and second execution

open a new session on workbench and trying to run this Query this query returns count '15' at first execution and '4' ata its second execution why is it so ..

select count(id) 
from (
   select *,@num := if(@company_id = company_id, @num + 1, 1) as row_number,@company_id := company_id as dummy 
   from salebuild_ctl.company_contact 
   where id in (12352,59898,59899,59900,59901,59902,59903,59904,14047,15196,15197,41402,41403,41404,41405)  
   order by company_id,  date_created asc  
) as x 
where x.row_number <= 2; 

Upvotes: 0

Views: 94

Answers (1)

Meherzad
Meherzad

Reputation: 8553

The reason is because first time you use SET @num=0 but for next time when running query you are again not setting it to 0 so the for the second run it takes @num=15.

Do for running correctly before 2nd run SET @num=0

Upvotes: 1

Related Questions