在 yii2 中使用限制范围?

2022-01-04 00:00:00 database php mysql yii2 yii

我想使用 limit 12,20 从数据库获取数据.

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

这是我的代码:

  $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(); 

它不起作用.它给了我所有的行.我也试过 ->limit([12,20]),没有用.

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

但是当我使用 limit(12) 时,我得到了 12 行.

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

我想在 limit 12,20 中获取行.在我的这段代码中,我应该怎么做?

I want to get rows in limit 12,20 . What should I have to do for that in my this 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(20)
->offset(12)
->groupBy('um.id')
->orderBy(['um.id' => SORT_DESC]);

Offset() 指定起始点,limit() 指定记录数.如果您想要 1220 之间的记录,请使用 limit(8).

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

更多信息:

  • http://www.bsourcecode.com/yiiframework2/select-query-模型/#offset
  • http://www.yiiframework.com/doc-2.0/yii-db-querytrait.html#offset%28%29-detail

相关文章