###

这是这几天,碰到的一个比较头疼的问题

使用union all联合查询,同时laravel 生成分页,但发生报错?

QueryException in Connection.php line 729:
SQLSTATE[]: Cardinality violation: 1222 The used SELECT statements have a different number of columns (SQL: (select count(*) as aggregate from (select * from `orders` where (`status` = completed and `refund_status` in (refunded, not_apply)) or `refund_status` = refunded) as orders left join `users` on `users`.`id` = `orders`.`user_id` left join `order_products` on `order_products`.`order_id` = `orders`.`id` where exists (select 1 from `order_products` where exists (select 1 from `products` where products.id = order_products.product_id) and order_products.order_id = orders.id) and exists (select ....

###

由于使用联合查询跟使用paginate()来生成分页,导致此问题,解决方法是,先获取到数据,就先不用直接调用分页函数,然后在进行分页处理就可以

在stackoverflow ,找到的问题解决方式

https://stackoverflow.com/questions/25338456/laravel-union-paginate-at-the-same-time

使用这个方法处理 https://laracasts.com/discuss/channels/general-discussion/paginator-class-is-missing

在我决解过程中,会需要引入相关到类

use Illuminate\Support\Facades\Input;
use Illuminate\Pagination\LengthAwarePaginator;

       // 处理分页问题
$page = Input::get('page', 1);
// Number of items per page
perPage = 5;
// Start displaying items from this number;
$offSet = ($page * $perPage) - $perPage;
// Get only the items you need using array_slice (only get 10 items since that's what you need)
$itemsForCurrentPage = array_slice($report_infos, $offSet, $perPage, true);
// Return the paginator with only 10 items but with the count of all items and set the it on the correct page
$report_infos = new LengthAwarePaginator($itemsForCurrentPage, count($report_infos), $perPage, $page, ['path' => request()->url(), 'query' => request()->query()]);
注意⚠️:当我们需要跳转页面当时候,有时我们是还有其他参数当,所以需要传入当前url,跟查询参数
['path' => request()->url(), 'query' => request()->query()]

Laravel - Union + Paginate at the same time? and another problem----1222 The used SELECT statements have a different number of columns (SQL: (select count(*) as aggregate from的更多相关文章

  1. SQL UNION 和 UNION ALL 操作符\SQL SELECT INTO 语句\SQL CREATE DATABASE 语句

    SQL UNION 操作符 UNION 操作符用于合并两个或多个 SELECT 语句的结果集. 请注意,UNION 内部的 SELECT 语句必须拥有相同数量的列.列也必须拥有相似的数据类型.同时,每 ...

  2. 梨子带你刷burp练兵场(burp Academy) - 服务器篇 - Sql注入 - SQL injection UNION attack, determining the number of columns returned by the query

    目录 SQL injection UNION attack, determining the number of columns returned by the query SQL injection ...

  3. 关于laravel 用paginate()取值取不到的问题

    前几天在写api的时候,出现了一个比较奇怪的问题,用paginate()方法取值取不到的问题,我奇怪的是,我用paginate()方法取值是直接复制粘贴之前自己写过的api中的代码的,怎么突然取不到了 ...

  4. laravel执行数据库迁移的过程中出现Illuminate\Database\QueryException : SQLSTATE[HY000] [2002] Operation timed out (SQL: select * from information_schema.tables where table_schema = shop and table_name = migrations

    向customers表添加字段phone php artisan make:migration add_phone_to_customers_table 问题: 解决方法: 将DB_HOST配置项修改 ...

  5. MySQL查询命令_SELECT 子查询

    首先创建一个table mysql> create table Total (id int AUTO_INCREMENT PRIMARY KEY,name char(20),stu_num in ...

  6. sql注入理解

    一.SQL注入产生的原因和危害 1.原因 SQL注入攻击指的是通过构建特殊的输入作为参数传入Web应用程序.而这些输入大都是SQL语法里的一些组合,通过执行SQL语句进而执行攻击者所要的操作,其主要原 ...

  7. MySQL笔记-union

    union语法 select ... union [all | distinct] selct ... union用于把来自多个select语句的结果组合在一个结果集中. 两次查询的列表必须相同,否则 ...

  8. mysql中UNION ALL用法

    MYSQL中的UNION UNION在进行表链接后会筛选掉重复的记录,所以在表链接后会对所产生的结果集进行排序运算,删除重复的记录再返回结果. 举例说明: select * from table1 u ...

  9. Mysql只Union用法

    MYSQL中的UNION UNION在进行表链接后会筛选掉重复的记录,所以在表链接后会对所产生的结果集进行排序运算,删除重复的记录再返回结果. 举例说明: select * from table1 u ...

随机推荐

  1. npm scripts 脚本基础指南

    什么是npm脚本? npm 允许在package.json文件里面,使用scripts字段定义脚本命令. 初始化package.json -> npm init -> 经历一系列的问答即可 ...

  2. python代码块和小数据池

    id和is 在介绍代码块之前,先介绍两个方法:id和is,来看一段代码 # name = "Rose" # name1 = "Rose" # print(id( ...

  3. 细说tomcat之session持久化探秘

    业务场景:通常,我们会在会话级别存放一些参数,期望在session生命周期内,可以一直取得保存在session中的指定数据:而只要session过期或者失效,则需要执行重新登录等操作.但是!我们对于这 ...

  4. 【noip 2016】普及组

    T1.买铅笔 题目链接 #include<cstdio> #include<algorithm> #include<cstring> using namespace ...

  5. PySpider 爬虫系统

    PySpider:一个国人编写的强大的网络爬虫系统并带有强大的WebUI.采用Python语言编写,分布式架构,支持多种数据库后端,强大的WebUI支持脚本编辑器,任务监视器,项目管理器以及结果查看器 ...

  6. linux 共享内存的理解

    1.  共享内存是在shmget时创建出来的, 在 shmget(USER_SHARE_MEM_KEY,  sizeof(ADDR_NODE) , IPC_CREAT|0666)) ==-1) 的 s ...

  7. 使用Excel过滤重复数据

    如题 由于数据太多,没有办法人工过滤,所以借助Excel 我的数据是这样的 需要找出里面重复的数据 首先,选中需要筛选的数据,点击开始 --> 条件格式 --> 突出显示单元格规则 --& ...

  8. .net+mvc,ueditor

    .net+mvc的百度编辑器ueditor 一.下载百度编辑器:http://ueditor.baidu.com/website/download.html 选择.net版本 二.解压后在mvc项目中 ...

  9. VMware 11安装Mac和Linux

    VMware 11安装Mac OS X 10.10_百度经验 https://jingyan.baidu.com/article/ff411625b9011212e48237b4.html?qq-pf ...

  10. Oracle 11g 的 自动内存管理

    oracle11g 设置memory_target的值,开启AMM(Auto Memory Management),剩下的Oracle就可以自动维护了 参考:1.https://www.cnblogs ...