Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use limit range in yii2?

I want to get data from db using limit 12,20 .

Here is my code:

  $Query = new Query;
   $Query->select(['um.id as USERid', 'um.first_name', 'um.last_name',   'um.email', 'COUNT(g.id) as guestCount'])
 ->from('user_master um')
 ->join('LEFT JOIN', 'guest g', 'g.user_id = um.id')
 ->limit(12,20)
 ->groupBy('um.id')
 ->orderBy(['um.id' => SORT_DESC]);

  $command = $Query->createCommand();
  $evevtsUserDetail = $command->queryAll(); 

It is not working. It is giving me all rows. I also tried ->limit([12,20]), not working.

But when I am using limit(12) then I am getting 12 rows.

I want to get rows in limit 12,20 . What should I have to do for that in my this code?

like image 304
unknownbits Avatar asked Aug 22 '15 12:08

unknownbits


1 Answers

Try this:

$Query = new Query;
$Query->select(['um.id as USERid', 'um.first_name', 'um.last_name','um.email','COUNT(g.id) as guestCount'])
->from('user_master um')
->join('LEFT JOIN', 'guest g', 'g.user_id = um.id')
->limit(20)
->offset(12)
->groupBy('um.id')
->orderBy(['um.id' => SORT_DESC]);

Offset() specifies the starting point and limit() specifies the Number of records. If you want records between 12 and 20 then use limit(8).

For More Info:

  • http://www.bsourcecode.com/yiiframework2/select-query-model/#offset
  • http://www.yiiframework.com/doc-2.0/yii-db-querytrait.html#offset%28%29-detail
like image 194
Insane Skull Avatar answered Oct 13 '22 15:10

Insane Skull