当前位置:网站首页>[Mysql] LEFT函数 | RIGHT函数
[Mysql] LEFT函数 | RIGHT函数
2022-04-23 03:09:00 【山茶花开时。】
1.LEFT函数
LEFT函数用于从给定字符串的左侧提取指定数量的字符
语法结构
LEFT(str,len)
str: 给定的字符串,将从其左侧提取字符
len: 要提取的字符数,如果此参数大于字符串中的字符数,则此函数将返回实际的字符串
注意: 如果任一参数为Null,结果返回Null
示例
-- 结果为空
SELECT LEFT('abcdefg',0);
-- abc
SELECT LEFT('abcdefg',3);
-- abcdefg
SELECT LEFT('abcdefg',10);
-- Null
SELECT LEFT('abcd',NULL);
-- LEFT函数可以截取数字
-- 12
SELECT LEFT(123456,2);
2.RIGHT函数
RIGHT函数用于从给定字符串的右侧提取指定数量的字符
语法结构
RIGHT(str,len)
str: 给定的字符串,将从其右侧提取字符
len: 要提取的字符数,如果此参数大于字符串中的字符数,则此函数将返回实际的字符串
注意: 如果任一参数为Null,结果返回Null
示例
-- 结果为空
SELECT RIGHT('abcdefg',0);
-- efg
SELECT RIGHT('abcdefg',3);
-- abcdefg
SELECT RIGHT('abcdefg',10);
-- Null
SELECT RIGHT('abcd',NULL);
-- RIGHT函数可以截取数字
-- 56
SELECT RIGHT(123456,2);
练习案例
导入数据
DROP TABLE IF EXISTS `employee_info`;
CREATE TABLE `employee_info` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`phone` varchar(255) DEFAULT NULL,
`salary` int(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4;
INSERT INTO `employee_info` VALUES ('1', 'Odin', '13810809401', '10000');
INSERT INTO `employee_info` VALUES ('2', 'Kacky', '15820126789', '12000');
INSERT INTO `employee_info` VALUES ('3', 'Jerry', '15815810158', '11000');
INSERT INTO `employee_info` VALUES ('4', 'Harry', '15820176889', '13000');
INSERT INTO `employee_info` VALUES ('5', 'Sun', '13578945621', '9000');
employee_info表

问题: 将员工手机号码进行加密处理(例如:138****9401) ,按薪资升序进行name,phone,salary格式输出数据
SELECT name, CONCAT(LEFT(phone,3),'****',RIGHT(phone,4))AS phone, salary
FROM employee_info
ORDER BY salary ASC;
结果展示:

版权声明
本文为[山茶花开时。]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Hudas/article/details/124342690
边栏推荐
- 全网讲的最细,软件测试度量,怎样优化软件测试成本提高效率---火爆
- The most easy to understand service container and scope of dependency injection
- 数据挖掘系列(3)_Excel的数据挖掘插件_估计分析
- c#语法糖模式匹配【switch 表达式】
- .NET7之MiniAPI(特别篇):.NET7 Preview3
- 2022年度Top9的任务管理系统
- [format] simple output (2)
- 2022T电梯修理考试模拟100题及在线模拟考试
- 腾讯视频涨价:一年多赚74亿!关注我领取腾讯VIP会员,周卡低至7元
- C syntax sugar empty merge operator [?] And null merge assignment operator [? =]
猜你喜欢

Openfeign timeout setting

Opencv combines multiple pictures into video

Aspnetcore configuration multi environment log4net configuration file

Notes sur le développement de la tarte aux framboises (XII): commencer à étudier la suite UNO - 220 de la tarte aux framboises de contrôle industriel advantech (i): Introduction et fonctionnement du s

編碼電機PID調試(速度環|比特置環|跟隨)

Source generator actual combat

ASP.NET 6 中间件系列 - 条件中间件

Summary of software test interview questions

AspNetCore配置多环境log4net配置文件

准备一个月去参加ACM,是一种什么体验?
随机推荐
数据挖掘系列(3)_Excel的数据挖掘插件_估计分析
先中二叉建树
Due to 3 ²+ four ²= five ², Therefore, we call '3,4,5' as the number of Pythagorean shares, and find the array of all Pythagorean shares within n (including n).
.NET点滴:说说Middleware构造中获取不到Scoped服务的问题
MYSQL_ From mastery to abandonment
使用栈来解决”迷你语法分析器“的问题
【鉴权/授权】自定义一个身份认证Handler
Tencent video price rise: earn more than 7.4 billion a year! Pay attention to me to receive Tencent VIP members, and the weekly card is as low as 7 yuan
2022G2电站锅炉司炉考试题库及在线模拟考试
Summary of interface automation interview questions for software testing
If the deep replication of objects is realized through C #?
編碼電機PID調試(速度環|比特置環|跟隨)
What kind of experience is it to prepare for a month to participate in ACM?
腾讯视频涨价:一年多赚74亿!关注我领取腾讯VIP会员,周卡低至7元
Blazor University (12) - component lifecycle
ASP.NET和ASP.NETCore多环境配置对比
2022A特种设备相关管理(电梯)上岗证题库及模拟考试
Openfeign details show
Yes Redis using distributed cache in NE6 webapi
MYSQL03_ SQL overview, rules and specifications, basic select statements, display table structure