Oracle和MySQL多表条件分页查询的高效SQL语句、MySQL分页查询总数total的获取

Oracle数据库分页查询:

利用rownumbetween and关键字

-- 查询员工表和薪水表的分页sql(pageNo:页号从1开始,pageSize:每页大小)
select* 
from(selectROWNUM rNo,user_id,user_name,user_dept,user_salary from(selectinfo.user_name,salary.* fromzdy_userinfo info,zdy_salary salary whereinfo.user_id = salary.user_id order byinfo.user_id ) where ROWNUM <= pageSize * pageNo order by ROWNUM asc
) 
where rNo between pageSize * (pageNo - 1) + 1 and pageSize * pageNo;

 

 MySQL数据库分页查询:

利用两个limit关键字和子查询的方式(单表,海量数据时性能显著提升)

-- 查询第2页的数据(假设每页10条)
select* 
fromt_course as t_cou
where t_cou.id >= (-- 第11条数据的idselect idfromt_course as t_cou2order by t_cou2.id limit 10, 1-- order by t_cou2.id limit pageSize * (pageNo - 1), 1)
order by id limit 10;
-- order by id limit pageSize;

MySQL实现Oracle中的rownum关键字的功能

SELECT@rownum := @rownum + 1 AS rownum,t_course.* 
FROM(SELECT @rownum := 0) r, -- 多表的别名r不能省略(多表结构完整)t_course;

 

MySQL中分页总数据条数Total的处理:

利用sql_calc_found_rows关键字配合found_rows()函数(性能较高,需要在连接参数中指定allowMultiQueries=true即支持一次执行多条SQL语句)

SELECT SQL_CALC_FOUND_ROWS
t_cou.id,
t_cou.title,
t_cou.STATUS,
t_cou.price,
t_tea.NAME AS teacherName,
t_tea.intro 
FROMt_course t_couLEFT OUTER JOIN t_teacher t_tea ON t_cou.teacher_id = t_tea.id 
WHERE1 = 1 AND t_cou.is_deleted <> 1 AND t_cou.price >= 0 AND t_cou.price <= 99 
ORDER BYt_cou.gmt_modified DESC LIMIT 0,3;
SELECTfound_rows() AS total;

Mybatis或Mybatis-Plus中使用示例:

    <resultMap id="courseSearchResultMap" type="com.wondersgroup.edu.entity.vo.CourseSearchResultVo"><id property="id" column="id"></id><result property="title" column="title"></result><result property="status" column="status"></result><result property="teacherName" column="teacherName"></result><result property="price" column="price"></result><result property="intro" column="intro"></result></resultMap><resultMap id="countResultMap" type="Long"><result property="count" column="total"></result></resultMap><!--条件分页查询课程列表信息--><!--由于是多参数传入,所以不需要对parameterType进行配置--><select id="getListCourse" resultMap="courseSearchResultMap, countResultMap"><!--<bind name="offset" value="(current - 1) * size"></bind>-->selectsql_calc_found_rowst_cou.id,t_cou.title,t_cou.status,t_cou.price,t_tea.name as teacherName,t_tea.introfromt_course t_couleft outer join t_teacher t_tea on t_cou.teacher_id = t_tea.idwhere 1 = 1 and t_cou.is_deleted &lt;&gt; 1<if test="courseSearchVo.title != null and courseSearchVo.title != ''">and t_cou.title like concat('%', #{courseSearchVo.title}, '%')</if><if test="courseSearchVo.status != null and courseSearchVo.status != ''">and t_cou.status = #{courseSearchVo.status}</if><if test="courseSearchVo.teacherName != null and courseSearchVo.teacherName != ''">and t_tea.name like concat('%', #{courseSearchVo.teacherName}, '%')</if><if test="courseSearchVo.priceLow != null and courseSearchVo.priceLow != ''">and t_cou.price &gt;= #{courseSearchVo.priceLow}</if><if test="courseSearchVo.priceHigh != null and courseSearchVo.priceHigh != ''">and t_cou.price &lt;= #{courseSearchVo.priceHigh}</if>order by t_cou.gmt_modified desc limit ${(current - 1) * size}, #{size};select found_rows() as total;</select>

服务端获取分页数据集和分页信息:

        List<List<?>> pageList = (List<List<?>>)courseService.pageQuery(courseSearchVo, current, size);// 分页total采用mysql提供的sql_calc_found_rows关键字配合found_rows()函数List<CourseSearchResultVo> dataList = (List<CourseSearchResultVo>) pageList.get(0);Long total = (Long) pageList.get(1).get(0);

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440198.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

[通俗易懂]深入理解TCP协议(下):RTT、滑动窗口、拥塞处理

转自即时通讯网&#xff1a;http://www.52im.net/ 前言 此文为系列文章的下篇&#xff0c;如果你对TCP不熟悉的话&#xff0c;请先看看上篇《[通俗易懂]深入理解TCP协议&#xff08;上&#xff09;&#xff1a;理论基础》 。 上篇中&#xff0c;我们介绍了TCP的协议头、状态机…

脑残式网络编程入门(一):跟着动画来学TCP三次握手和四次挥手

转自即时通讯网&#xff1a;http://www.52im.net/ 1、引言 网络编程中TCP协议的三次握手和四次挥手的问题&#xff0c;在面试中是最为常见的知识点之一。很多读者都知道“三次”和“四次”&#xff0c;但是如果问深入一点&#xff0c;他们往往都无法作出准确回答。 本篇文章尝…

【HDU - 5452】Minimum Cut(树形dp 或 最近公共祖先lca+树上差分,转化tricks,思维)

题干&#xff1a; Given a simple unweighted graph GG (an undirected graph containing no loops nor multiple edges) with nn nodes and mm edges. Let TT be a spanning tree of GG. We say that a cut in GG respects TT if it cuts just one edges of TT. Since love…

Idea自带的工具打jar包和Maven打Jar包(SpringBoot工程)

1.Idea自带的工具打jar包 &#xff08;1&#xff09;点击菜单栏的File后选中Project Structure&#xff0c;接着按如下图所示操作&#xff1a; &#xff08;2&#xff09;点击“OK”按钮后会出现下图的界面&#xff0c;然后继续点击“OK”按钮 &#xff08;3&#xff09;现在开…

图解算法学习笔记(目录)

今天遇到一本好书&#xff0c;如下&#xff0c;书很薄&#xff0c;不到200页&#xff0c;有将近400张图片&#xff0c;算法介绍的很有趣。这也是我读的第三本袁国忠先生翻译的书&#xff0c;向两位致敬。 目录大致如下; 第1章&#xff1a;二分查找和大O表示法&#xff1b; 第…

【POJ - 3249】Test for Job(DAG线性求带负权的最长路,dp)

题干&#xff1a; Mr.Dog was fired by his company. In order to support his family, he must find a new job as soon as possible. Nowadays, Its hard to have a job, since there are swelling numbers of the unemployed. So some companies often use hard tests for …

图解算法学习笔记(二): 选择排序

目录 1)数组和链表&#xff1a; 2)选择排序算法&#xff1a; 3)小结 本章内容&#xff1a; 两种基本数据结构&#xff1a;数组和链表&#xff1b; 选择排序算法&#xff1b; 1)数组和链表&#xff1a; 数组是连续的内存单元&#xff0c;链表可以不连续&#xff1b; 链表…

【HDU - 5456】Matches Puzzle Game(数位dp,思维)

题干&#xff1a; As an exciting puzzle game for kids and girlfriends, the Matches Puzzle Game asks the player to find the number of possible equations A−BCA−BC with exactly n (5≤n≤500)n (5≤n≤500) matches (or sticks). In these equations, A,BA,B and …

图解算法学习笔记(三):递归

本章内容&#xff1a; 学习递归&#xff1b;如何将问题分解成基线条件和递归条件。 1) 每个递归函数都有两部分&#xff1a;基线条件(base case)和递归条件(recursive base)。例如&#xff1a;打印3...2...1 def countdown(i):print(i)if i < 0:returnelse:countdown(i…

Apollo自动驾驶入门课程第⑨讲 — 控制(上)

目录 1. 简介 2. 控制流程 3. PID控制 4. PID优劣对比 本文转自微信公众号&#xff1a;Apollo开发者社区 原创&#xff1a; 阿波君 Apollo开发者社区 9月26日 上周我们发布了无人驾驶技术的 规划篇&#xff0c;车辆基于高精地图&#xff0c;感知和预测模块的数据来进行这一…

Apollo自动驾驶入门课程第⑩讲 — 控制(下)

目录 1. 线性二次调节器 2. 模型控制预测 3. 总结 本文转自微信公众号&#xff1a;Apollo开发者社区 原创&#xff1a; 阿波君 Apollo开发者社区 昨天 Apollo自动驾驶课程马上进入尾声&#xff0c;在无人驾驶技术控制篇&#xff08;上&#xff09;中&#xff0c;具体讲解了最…

图解算法学习笔记(四):快速排序

目录 1&#xff09; 示例1&#xff1a; 2&#xff09;快速排序 3) 再谈大O表示法 4&#xff09;小结 本章内容&#xff1a;学习分而治之&#xff0c;快速排序 1&#xff09; 示例1&#xff1a; 假设你是农场主&#xff0c;有一小块土地&#xff0c;你要将这块地均匀分成方…

图解算法学习笔记(五):散列表

目录 1&#xff09;示例1&#xff1a; 2&#xff09;散列函数 3&#xff09;应用案例 4&#xff09;冲突 5&#xff09;性能 6&#xff09;小结 本章内容&#xff1a; 学习散列表&#xff0c;最有用的数据结构之一。 学习散列表的内部机制&#xff1a;实现、冲突和散列函…

图解算法学习笔记(六):广度优先搜索

目录 1&#xff09;图简介 2&#xff09;图是什么 3&#xff09;广度优先搜索 4&#xff09;实现图 5&#xff09;实现算法 6&#xff09;小结 本章内容; 学习使用新的数据结构图来建立网络模型&#xff1b; 学习广度优先搜索&#xff1b; 学习有向图和无向图…

图解算法学习笔记(七):狄克斯特拉算法

目录 1&#xff09;使用狄克斯特拉算法 2&#xff09;术语 3&#xff09;实现 4&#xff09;小结 本章内容; 介绍加权图&#xff0c;提高或降低某些边的权重&#xff1b; 介绍狄克斯特拉算法&#xff0c;找出加权图中前往X的最短路径&#xff1b; 介绍图中的环…

【HDU - 5477】A Sweet Journey(思维,水题)

题干&#xff1a; Master Di plans to take his girlfriend for a travel by bike. Their journey, which can be seen as a line segment of length L, is a road of swamps and flats. In the swamp, it takes A point strengths per meter for Master Di to ride; In the f…

图解算法学习笔记(八):贪婪算法

目录 &#xff08;1&#xff09;背包问题 &#xff08;2&#xff09;集合覆盖问题 &#xff08;3&#xff09;NP完全问题 &#xff08;4&#xff09;小结 本章内容&#xff1a; 学习如何处理没有快速算法的问题&#xff08;NP完全问题&#xff09;。学习近似算法&#xff…

图解算法学习笔记(九):动态规划

目录 &#xff08;1&#xff09;背包问题 &#xff08;2&#xff09;最长公共子串 &#xff08;3&#xff09;小结 本章内容&#xff1a; 学习动态规划&#xff0c;它将问题分成小问题&#xff0c;并先着手解决这些小问题。学习如何设计问题的动态规划解决方案。 &#xff…

Java(win10安装jdk,第一个hello world)

Java 第一步 &#xff1a;安装jdk 推荐默认安装。&#xff08;安装到C盘&#xff09;第二步 &#xff1a;配置jdk环境 JAVA_HOME C:\Program Files\Java\jdk1.8.0_191 JDK的安装路径 Path&#xff1a; C:\Program Files\Java\jdk1.8.0_191\bin JDK下bin目录的路径 &#xf…

#pragma 详解

#pragma 求助编辑 pragma - 必应词典美[prɡmə]英[prɡmə]n.〔计〕杂注网络编译指示&#xff1b;显示编译指示&#xff1b;特殊指令 百科名片 在所有的预处理指令中&#xff0c;#Pragma 指令可能是最复杂的了&#xff0c;它的作用是设定编译器的状态或者是指示编译器完成一些…