以Yii2的形式订购两次

以Yii2的形式订购两次,yii2,Yii2,如何在yii2中按同一列订购两次 select * from ( select * from your_table order by id desc limit 20) tmp order by tmp.id asc 为什么我需要排序两次? 我有活动表。我想显示其eventDate大于今天的事件,然后使用asc对其进行排序 $events = Events::find()->where(['>=', 'eventDate', $today])->andWhere(['

如何在yii2中按同一列订购两次

select * from (
   select * from your_table order by id desc limit 20) 
tmp order by tmp.id asc
为什么我需要排序两次? 我有活动表。我想显示其eventDate大于今天的事件,然后使用asc对其进行排序

$events = Events::find()->where(['>=', 'eventDate', $today])->andWhere(['status' => 1])->orderBy(['eventDate' => SORT_ASC])->limit(5)->all();
因为我想显示至少5个事件,如果上面查询的结果小于5,我需要查找eventDate小于今天的所有事件,并按降序排序,但我想使用升序显示

if(count($events)<4){
        $need = 4 - count($events);
        $pastEvents = Events::find()->where(['<', 'eventDate', $today])->andWhere(['status' => 1])->orderBy(['eventDate' => SORT_DESC])->limit($need)->all();

    }

if(count($events),其中(['一种简单的方法是使用findBySql

$sql = "select * from (
   select * from your_table order by id desc limit 20) 
   tmp 
   order by tmp.id asc"

YourTableOrderMOdel::findBySql()->count( $sql)->all();

一种简单的方法是使用findBySql

$sql = "select * from (
   select * from your_table order by id desc limit 20) 
   tmp 
   order by tmp.id asc"

YourTableOrderMOdel::findBySql()->count( $sql)->all();

您使用的是子查询,因此可以使用用于简单查询的所有内容

$subQuery = (new Query())->select('id')->from('user')->where('status=1')->orderBy('id');

$query->from(['u' => $subQuery])->orderBy('u.id');

您使用的是子查询,因此可以使用用于简单查询的所有内容

$subQuery = (new Query())->select('id')->from('user')->where('status=1')->orderBy('id');

$query->from(['u' => $subQuery])->orderBy('u.id');

你能解释一下为什么要排序两次吗。?