在MariaDB数据库中如何对检索记录做降序升序操作
Admin 2022-06-24 群英技术资讯 1605 次浏览
关于“在MariaDB数据库中如何对检索记录做降序升序操作”的知识有一些人不是很理解,对此小编给大家总结了相关内容,具有一定的参考借鉴价值,而且易于学习与理解,希望能对大家有所帮助,有这个方面学习需要的朋友就继续往下看吧。在MariaDB数据库中,使用SELECT语句和LIMIT子句从表中检索一个或多个记录。
语法:
SELECT expressions
FROM tables
[WHERE conditions]
[ORDER BY expression [ ASC | DESC ]]
LIMIT row_count;
示例1:
以降序检索记录:
使用SELECT语句并带有LIMIT子句查询students表中的数据。结果student_id列的值按降序显示,LIMIT为3。参考如下语句 -
SELECT student_id, student_name, student_address
FROM Students
WHERE student_id <= 7
ORDER BY student_id DESC
LIMIT 3;
执行上面查询语句,得到以下结果 -
MariaDB [testdb]> SELECT student_id, student_name, student_address
-> FROM Students
-> WHERE student_id <= 7
-> ORDER BY student_id DESC
-> LIMIT 3;
+------------+--------------+-----------------+
| student_id | student_name | student_address |
+------------+--------------+-----------------+
| 6 | Blaba | Shengzheng |
| 5 | Kobe | Shanghai |
| 4 | Mahesh | Guangzhou |
+------------+--------------+-----------------+
3 rows in set (0.00 sec)
示例2:
按student_id列的值升序检索记录:
SELECT student_id, student_name, student_address
FROM Students
WHERE student_id <= 7
ORDER BY student_id ASC
LIMIT 3;
执行上面查询语句,得到以下结果 -
MariaDB [testdb]> SELECT student_id, student_name, student_address
-> FROM Students
-> WHERE student_id <= 7
-> ORDER BY student_id ASC
-> LIMIT 3;
+------------+--------------+-----------------+
| student_id | student_name | student_address |
+------------+--------------+-----------------+
| 1 | Maxsu | Haikou |
| 3 | JMaster | Beijing |
| 4 | Mahesh | Guangzhou |
+------------+--------------+-----------------+
3 rows in set (0.00 sec)
示例3:分页
在应用程序中,由于数据记录太多,不能全在一个页面中全部显示,我们经常要使用分页来显示。假设每页显示3条记录,参考以下语句 -
-- 第1页数据
SELECT student_id, student_name, student_address
FROM Students
WHERE student_id > 0
ORDER BY student_id ASC
LIMIT 0,3;
-- 第2页数据
SELECT student_id, student_name, student_address
FROM Students
WHERE student_id > 0
ORDER BY student_id ASC
LIMIT 3,3;
-- 第3页数据
SELECT student_id, student_name, student_address
FROM Students
WHERE student_id > 0
ORDER BY student_id ASC
LIMIT 6,3;
执行上面查询语句,得到以下结果 -
MariaDB [testdb]> SELECT student_id, student_name, student_address
-> FROM Students
-> WHERE student_id > 0
-> ORDER BY student_id ASC
-> LIMIT 0,3;
+------------+--------------+-----------------+
| student_id | student_name | student_address |
+------------+--------------+-----------------+
| 1 | Maxsu | Haikou |
| 3 | JMaster | Beijing |
| 4 | Mahesh | Guangzhou |
+------------+--------------+-----------------+
3 rows in set (0.05 sec)
MariaDB [testdb]> SELECT student_id, student_name, student_address
-> FROM Students
-> WHERE student_id > 0
-> ORDER BY student_id ASC
-> LIMIT 3,3;
+------------+--------------+-----------------+
| student_id | student_name | student_address |
+------------+--------------+-----------------+
| 5 | Kobe | Shanghai |
| 6 | Blaba | Shengzheng |
+------------+--------------+-----------------+
2 rows in set (0.00 sec)
MariaDB [testdb]>据
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
在本章中,我们将学习如何在表中插入数据。小编觉得挺不错的,对大家学习或是工作可能会有所帮助,对此分享发大家做个参考,希望这篇文章能帮助大家解决问题。
MariaDB UNION ALL操作符与UNION操作符相同,但不会删除重复的记录。它返回查询中的所有行,并且不删除各种SELECT语句之间的重复行。
这篇文章给大家分享的是MariaDB中update用法。小编觉得挺实用的,因此分享给大家做个参考,对大家学习MariaDB数据库会有帮助,文中的示例介绍得很详细,有需要的朋友可以参考,接下来就跟随小编一起了解看看吧。
MariaDB SUM()函数用于返回表达式求和的值。
MariaDB中怎么样恢复数据库?为了确保我们数据库的数据安全,很多朋友有数据库备份的习惯,而备份的方法有很多,这篇主要给大家介绍的是如何备份中恢复数据库。其实恢复数据库并不难,但是如果数据量大,那么恢复过程就非常漫长。
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008