-
最近面试被问到,mysql对表按指定字段排序后,取第11条记录sql怎么写?
-
以前用的sql server 想到取前10记录用的是top10,对于mysql也套用的,原来是不对的,尴尬!
-
以一个学生表student为例,按分数score排序。
-
取排序后的第11条记录:
select * from student order by score limit 10,1; -
直接取排序后的前10条记录:
select * from student order by score limit 10;
这样的也可以 select * from student order by score limit 0,10; -
Mysql中的一些函数也是非常让人惊艳的,在进行查询或者造数据的时候都用得上。以员工表emp为例,手机号字段为phone
1、substring 函数 从具有特定长度的位置开始提取一个字符串.
1)提取员工表手机号的后四位。
select substring(phone,8,4) from emp;
2、concat 将两个或多个字符串平成一个字符串。
1)将员工表中手机号的中间四位屏蔽。
select concat(substring(phone,1,3),’####’,substring(phone,8,4)) from emp.
结果显示手机号是这样的132####4520
3、replace 搜索并替换字符串中的子字符串
1)把员工表中前10条记录的手机号前三位换成135.
updata emp set phone=replace(phone,132,135) limit 10;
4、length 获取字符串的长度。
select length(phone) from emp.
5、count 统计表中记录条数。
select count(*) from emp。
6、trim()过滤指定的字符串。
1)ltrim(str)删除左空格
2)rtrim(str)删除右空格
3)trim(str)删除首尾的空格
7、now()获取当前系统时间
select now(); ==>2020-06-16 17:13:20
curdate()获取当前日期。
select curdate(); ===>2020-06-168、max求最大值,min最小值,sum求和这个比较简单就不举例子了。
Mysql对表排序后取指定的记录
最新推荐文章于 2022-06-27 13:23:30 发布