drnvcm3949 2015-05-13 10:22
浏览 195

如何使用Yii2 PHP框架将来自两个查询的两个分页合并为一个

I am executing one query that will return me 8 results. It is very complex, so I am not going to post it here, because maybe it doesn't matter, maybe what I am trying to do is not possible, but if it is, I hope someone can guide me. So first query returns 8 results, and I will grab the ID of each result row. I am doing this because in second query I need to use those Ids in WHERE NOT IN statement. I mean, second query will return 18 results, because 8 will be ignored since they was found with first query.

Let me explain further. I had one query that was returning me 26 results. But customer wanted to order those result by criteria that is not available if I do not execute second query and put results of that query on top of results list. I mean, instead of having one query and one result list, now I have two queries but their results should be combined into one list where results from the first query go on top. But there comes the problem with pagination.

Before I execute first query using Yii2 DAO, I am creating pagination object that has totalCount equal to 26 ( I have one count query that will return me how many results I have in total ). But my first query have 8 results, second got 18. If I limit page results display to 10, pagination will not work.

Here is some pseudo code that may help:

// search query that will return number of ids found by certain criteria
$count = $model->countOffers($condition);

// build pagination object
$pagination = new Pagination(['totalCount' => $count, 'pageSize' => 10]);

// execute first query that will return the  array of results
// inside this query $pagination object is used to build LIMIT and OFFSET
$firstQuery = $model->getFirst($condition, $pagination);

// get ID's that should be ignored in next query
foreach ($firstQuery as $data) 
{
    $ignoreIds[] = $data['id'];
}

// execute second query that will return results without $ignoredIds being used
// inside this query $pagination object is used to build LIMIT and OFFSET
// $secondQuery = $model->getSecond($condition, $pagination, $ignoredIds);

// render view and display pagination there, but pager is broken
$this->render('index', ['firstQuery' => $firstQuery, 'secondQuery' => $secondQuery, 'pagination' => $pagination]);

I am not giving you concrete code, because I am not sure if I am trying to do possible or impossible thing. So I would like if someone can give me a clue if this is possible to do, and if yes, what would be the steps to achieve it.

EDIT: this is the index view:

<?php if ($queryOne): ?>
    <?= $this->render('_index', ['data' => $queryOne]) ?>
<?php endif ?>

<?php if ($queryTwo): ?>
    <?= $this->render('_index', ['data' => $queryTwo]) ?>
<?php endif ?>
  • 写回答

1条回答 默认 最新

  • douliang4858 2015-07-10 11:27
    关注

    Instead to split into 2 different results, mark missing articles as "missing" using your complex query in a left join

    select if(missing_id is null, 1,0) missing, ...
    from articles as a
    left join (select id as missing_id ... your-complex-query) 
      as miss on a.id = miss.missing_id
    

    So you will have a single result of 26 lines, 8 with missing to 1 and the others with missing to 0.

    评论

报告相同问题?

悬赏问题

  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图