当前位置:网站首页>[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
边栏推荐
- be based on. NETCORE development blog project starblog - (1) why do you need to write your own blog?
- C#语法糖空合并运算符【??】和空合并赋值运算符【 ??=】
- 基于.NetCore开发博客项目 StarBlog - (1) 为什么需要自己写一个博客?
- Tencent video VIP member, weekly card special price of 9 yuan! Tencent official direct charging, members take effect immediately!
- Ningde's position in the times is not guaranteed?
- C语言实现通讯录----(静态版本)
- Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (8)
- First in the binary tree
- 7-11 rearrange the linked list (25 points)
- OLED多级菜单记录
猜你喜欢

MYSQL05_ Ordr by sorting, limit grouping, group by grouping

全网最全,接口自动化测试怎么做的?精通接口自动化测试详解

PID debugging of coding motor (speed loop | position loop | follow)

be based on. NETCORE development blog project starblog - (1) why do you need to write your own blog?

全网讲的最细,软件测试度量,怎样优化软件测试成本提高效率---火爆

准备一个月去参加ACM,是一种什么体验?

2022年度Top9的任务管理系统

Service avalanche effect

Development notes of raspberry pie (12): start Advantech industrial control raspberry pie uno-220 Kit (I): introduction and operation of the system

The most easy to understand service container and scope of dependency injection
随机推荐
How does Microsoft solve the problem of multiple PC programs
ASP. Net 6 middleware series - Custom middleware classes
C syntax pattern matching [switch expression]
Simple example of using redis in PHP
利用正反遍历来解决“字符的最短距离”问题
The backtracking of stack is used to solve the problem of "the longest absolute path of file"
树莓派开发笔记(十二):入手研华ADVANTECH工控树莓派UNO-220套件(一):介绍和运行系统
Use split to solve the "most common words" problem
C language to achieve address book - (static version)
Blazor University (12) - component lifecycle
7-11 重排链表 (25 分)
Fight leetcode again (290. Word law)
[new version release] componentone added Net 6 and blazor platform control support
Basic SQL (VIII) data update operation practice
[ncnn] - the meaning of - 23300 in param
Drawing polygons with < polygon / > circular array in SVG tag
How to write the expected salary on your resume to double your salary during the interview?
TP5 multi conditional where query (using PHP variables)
建立与遍历二叉树
使用栈来解决”迷你语法分析器“的问题