xutongle 2016-07-30 17:48:58 10772次浏览 10条评论 19 3 1
use app\models\User;
use yii\data\Pagination;

        $subQuery = User::find()->select('id')->where('id>0');
        $count = $subQuery->count();

        $pagination = new Pagination(['totalCount' => $count]);

        $subQuery->offset($pagination->offset)
            ->limit($pagination->limit);

        $query = User::find()->select('*')->from(['t1' => User::tableName(), 't2' => $subQuery])->where('t1.id=t2.id')->all();

        $articles = $query->offset($pagination->offset)
            ->limit($pagination->limit)
            ->all();

        print_r($articles);
觉得很赞
  • 评论于 2016-08-01 16:51 举报

    强强强!!!

  • 评论于 2016-08-02 14:35 举报

    很好,good

  • 评论于 2016-08-04 15:49 举报

    没看懂,为什么还要用from

    1 条回复
    评论于 2016-08-05 13:58 回复

    SQL原型 SELECT t1.* FROM item t1, (SELECT id FROM item WHERE id>100 LIMIT 10000,20 ) t2 WHERE t1.id=t2.id;
    懂了吧

    觉得很赞
  • 评论于 2016-08-05 13:57 举报
    $articles = $query->offset($pagination->offset)
                ->limit($pagination->limit)
                ->all();
    

    改成
    $articles = $query->all();
    发的匆忙写错了

  • 评论于 2017-11-01 16:45 举报

    厉害,你这个sql解决了我想单表联查的想法

  • 评论于 2017-11-01 16:58 举报

    大佬,求助个问题,我想单表联查,其中id和p_id关联,5de03fd70893cf6f45b347547ced026.png

    SELECT t1.,(select t3.name from users t3 where t3.id=t2.p_id) pname FROM users t1, (SELECT FROM users LIMIT 0,20 ) t2 WHERE t1.id=t2.id ;

    怎么把(select t3.name from users t3 where t3.id=t2.p_id)这条语句用yiiAR的方法写入呢,求指教

    1 条回复
    评论于 2017-11-02 10:28 回复

    用字符串硬插

  • 评论于 2018-02-26 15:15 举报

    厉害了我的哥

  • 评论于 2018-05-11 09:21 举报

    没看懂 哪里实用千万级数据的了

    2 条回复
    评论于 2018-09-29 10:17 回复

    同样是limit,你试试传统写法,和我SQL原型写法就知道了

    评论于 2018-09-29 10:25 回复

    SELECT t1.* FROM ip_taobao t1, (SELECT id FROM ip_taobao LIMIT 14461600,20 ) t2 WHERE t1.id=t2.id

    OK
    时间: 3.606s

    SELECT * FROM ip_taobao LIMIT 14461600, 100

    OK
    时间: 6.89s

    觉得很赞
  • 评论于 2018-05-12 19:13 举报

    mark了

  • 评论于 2018-08-03 16:40 举报

    不能表连接?

您需要登录后才可以评论。登录 | 立即注册