###

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

使用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. SpringBoot系列: 与Spring Rest服务交互数据

    不管是单体应用还是微服务应用, 现在都流行Restful风格,  下图是一个比较典型的使用rest的应用架构, 该应用不仅使用database数据源, 而且用到了一个Weather微服务, 另一方面, ...

  2. JS 样式字符串 转 JSON对象

    项目中需要把div 上的样式值转成数据展示 形如: padding: 7px 2px 1px 3px; color: rgb(238, 65, 65); background-color: rgb(2 ...

  3. Repeater中使用条件的两种方法

    1.使用三目运算符 display=<%#(Eval("Sex", "{0}") == "01") ? "none" ...

  4. 27. Spring Boot 缓存注解详解: @Cacheable、@CachePut、 @CacheEvict、@Caching、@CacheConfig

     1.使用OGNL的命名规则来定义Key的值 @Cacheable(cacheNames = {"user"},key = "#root.methodName + '[' ...

  5. alexnet- tensorflow

    alexnet 在 imagenet上夺冠是卷积神经网络如今这么火热的起点. 虽然卷积神经网络很早就被提出来,但是由于计算能力和各方面原因,没有得到关注. alexnet 为什么能取得这么好的成绩,它 ...

  6. sql 左右连接 on 之后的and 和where的区别

  7. C# CancellationTokenSource 终止线程 CancellationTokenSource实现对超时任务的取消

    C# 使用 CancellationTokenSource 终止线程 使用CancellationTokenSource对象需要与Task对象进行配合使用,Task会对当前运行的状态进行控制(这个不用 ...

  8. Centos6.8 下解决服务器被挖矿当肉鸡的方法

    刚上班发现有些服务跑不起来,进入服务器查看原因: 第一部分: 一,#top 因为是刚被我kill 掉一次,kill 掉等会还会自启动,之前yam 进程占cpu 是200% 二,# vim /etc/r ...

  9. 洛谷P1972 【[SDOI2009]HH的项链】

    这道题想了很久,发题解是为了理解的更深刻一点...(管理放我过好嘛qwq) 步入正题:这道题应该是很多做法,我选择的是离线+树状数组. 首先输入数组.用fisrt数组先记录元素最开始出现的位置,对应的 ...

  10. HashMap 底层分析

    以下基于 JDK1.7 分析 如图所示,HashMap底层是基于数组和链表实现的,其中有两个重要的参数: ---容量 ---负载因子 容量的默认大小是16,负载因子是0.75,当HashMap的siz ...