当前位置:网站首页>Built in functions for MySQL database operations
Built in functions for MySQL database operations
2022-06-28 02:59:00 【Test the prettiest guy】
Catalog
Preface :MySQL Built in functions can be select、where、order by Use... In the back
One 、 Data preparation
-- establish student library :student
create database student default character set utf8 collate utf8_general_ci;
/* Create student table :students, Student number is Sid, The data type is varchar(10), Primary key , The name is name, The data type is varchar(10), The gender is sex, The data type is varchar(1), Age is age, The data type is int, Class for class, The data type is varchar(10), The ID card number is card, The data type is varchar(20), The city is for city, The data type is varchar(20) */
create table students(
Sid varchar(10) primary key,
name varchar(10),
sex varchar(1),
age int,
class varchar(10),
card varchar(20),
city varchar(20)
);
-- Insert data into the student table
insert into students values
('001',' Wang Zhaojun ',' Woman ',30,'3 class ','634101199003157654',' Beijing '),
('002',' Zhugeliang ',' male ',29,'2 class ','110102199104262354',' Shanghai '),
('003',' Ruban master ',' male ',30,'1 class ','820102199003047654',' nanjing '),
('004',' White ',' male ',35,'4 class ','840202198505177654',' anhui '),
('005',' Big Joe ',' Woman ',28,'3 class ','215301199204067654',' tianjin '),
('006',' Sun shangxiang ',' Woman ',25,'1 class ','130502199506137654',' hebei '),
('007',' Hundred Li xuance ',' male ',39,'2 class ','140102198107277654',' shanxi '),
('008',' Little Joe ',' Woman ',25,'3 class ',null,' Henan '),
('009',' Hundred Li keep the promise ',' male ',31,'1 class ','',' hunan '),
('010',' Daji ',' Woman ',24,'2 class ','440701199607147654',' guangdong '),
('011',' Lian po ',' male ',30,'1 class ','110202199005017754',' Beijing '),
('012',' Sun Bin ',' male ',36,'3 class ','650102198401297655',' xinjiang ');
/* Create a curriculum :courses The course number is Cid, The data type is varchar(10), Primary key , The course is called Cname, The data type is varchar(10) */
create table courses(
Cid varchar(10) primary key,
Cname varchar(10)
);
-- Insert data into the curriculum
insert into courses values
('01',' Chinese language and literature '),
('02',' mathematics '),
('03',' English '),
('04',' biological '),
('05',' Politics '),
('06',' Geography '),
('07',' Physics ');
/* Create a score table :scores, Student number is Sid, The data type is varchar(10), Student table foreign keys , The course number is Cid, The data type is varchar(10), Curriculum foreign key , The score is score, The data type is tinyint */
create table scores(
Sid varchar(10),
Cid varchar(10),
score tinyint,
foreign key (Sid) references students(Sid),
foreign key (Cid) references courses(Cid)
);
-- Insert data into the score table
insert into scores values
('001','01',49),
('001','02',75),
('001','03',54),
('001','04',66),
('001','05',59),
('001','06',94),
('002','01',58),
('002','02',57),
('002','03',74),
('002','04',90),
('002','05',55),
('002','06',64),
('003','01',83),
('003','02',73),
('003','04',50),
('003','05',67),
('003','06',87),
('004','01',97),
('004','02',50),
('004','03',56),
('004','04',69),
('004','05',72),
('004','06',51),
('005','01',54),
('005','02',66),
('005','03',61),
('005','05',49),
('005','06',86),
('006','01',97),
('006','02',77),
('006','03',66),
('006','04',76),
('007','01',49),
('007','02',40),
('007','03',99),
('007','04',86),
('007','05',78),
('007','06',55),
('008','01',82),
('008','02',72),
('008','03',98),
('008','05',47),
('008','06',68),
('009','01',84),
('009','02',56),
('009','03',74),
('009','04',64),
('009','05',79),
('009','06',97),
('010','02',85),
('010','03',85),
('010','04',90),
('010','05',93),
('010','06',61),
('011','01',85),
('011','02',63),
('011','03',71),
('011','04',58),
('011','05',80),
('011','06',66);
Two 、 String function
1. Splice string function -concat
grammar :concat( Parameters 1, Parameters 2, Parameters 3....)
effect : Concatenate all the parameters into a complete string
Be careful : Parameters can be numbers , It could be a string
practice 1:
-- hold 12,34.5,‘abc1 in ’ Concatenate into a string
select concat(12,34.5,'abc1 in ');
practice 2:
-- hold students In the table name and age The field data is spliced into a string and displayed
select concat(name,age) from students;
2. Calculate the length of the string -length
grammar :length( Parameters )
effect : Calculate the length of the string
Be careful : One utf8 The length of Chinese characters in the format is 3
practice 1:
-- Calculation ‘123abc "Avatar" ’ Length of string
select length('123abc "Avatar" ');
practice 2:
-- Calculation students In the table name Length of field
select length(name) from students;
3. Intercepting string
Be careful : Here the length of Chinese characters and letters is 1; The intercept length can only be a positive integer
Left hand intercept
grammar :left(' character string ', Interception length )
effect : Intercepts the left part of a string of a specified length
practice 1:
-- Intercept ‘123abc "Avatar" ’ The left part of the string ‘123’
select left('123abc "Avatar" ',3);
practice 2:
-- Intercept students In the table name The length of the field is 9 The left part of the last name
select left(name,1) from students where length(name)=9;
Right intercept
grammar :right(' character string ', Interception length )
effect : Intercepts the right part of a string of specified length
practice 1:
-- Intercept ‘123abc "Avatar" ’ The right part of the string ‘ "Avatar" ’
select right('123abc "Avatar" ',3);
practice 2:
-- Intercept students In the table name The length of the field is 12 The name of the right part of
select right(name,2) from students where length(name)=12;
Intercept in the middle
grammar :substring(' character string ', Starting position , Interception length )
effect : Intercepts any part of a string of a specified length
Be careful : The start position can be a positive integer 、0、 Negtive integer ; The start position is contained in the intercepted
practice 1:
-- Intercept ‘123abc "Avatar" ’ A string of ‘abc’ part
select substring('123abc "Avatar" ',4,3);
practice 2:
-- Intercept students Tabular card Date of birth of the field
select substring(card,7,8) from students;
practice 3:
-- Inquire about students Student information in the table , And according to the birthday from big to small ( Tips 1989 Annual ratio 1990 Big year )
select * from students order by substring(card,7,8);
4. Remove the space
Remove the space on the left
grammar :ltrim(' character string ')
effect : Remove the space to the left of the string
practice 1:
-- Remove strings ‘ 123abc "Avatar" ’ The space on the left
select ltrim(' 123abc "Avatar" ');
Remove the space on the right
grammar :rtrim(' character string ')
effect : Remove the space to the right of the string
practice 1:
-- Remove strings ‘ 123abc "Avatar" ’ The space on the right
select rtrim(' 123abc "Avatar" ');
Remove the space on both sides
grammar :trim(' character string ')
effect : Remove the space around the string
practice 1:
-- Remove strings ‘ 123abc "Avatar" ’ Space on both sides
select trim(' 123abc "Avatar" ');
3、 ... and 、 Mathematical functions
1. rounding -round
grammar :round( Number to process , Keep a few decimal places )
effect : rounding , Keep several decimal places after the decimal point
practice 1:
-- 8.3579 rounding , Keep integer
select round('8.3579',0);
practice 2:
-- Inquire about students Table average age of middle school students , And keep 2 Decimal place
select round(avg(age),2) from students;
2. random number -rand
grammar :rand()
effect : Return a return from 0 To 1.0 A random fraction of
practice 1:
-- Return a return from 0 To 1.0 A random fraction of
select rand();
practice 2:
-- from students A student is randomly selected from the table ( Random sorting takes the first )
select * from students order by rand() limit 1;
Four 、 Date time function
1. The current date
grammar :current_date()
effect : View the current date ( Format :yyyy-mm-dd)
practice 1:
-- View the current date
select current_date();
2. current time
grammar :current_time()
effect : View current time ( Format :HH-mm-ss)
practice 1:
-- View current time
select current_time();
3. The current date + Time
grammar :now()
effect : View the current date + Time ( Format :yyyy-mm-dd HH-mm-ss)
practice 1:
-- View the current date + Time
select now();
边栏推荐
- 数据清洗工具flashtext,效率直接提升了几十倍数
- 分布式事务TCC浅析
- Opencv -- geometric space transformation (affine transformation and projection transformation)
- Is it safe for qiniu to open an account? How do I open an account online?
- [fuzzy neural network] simulation of fuzzy neural network based on MATLAB
- 【Kotlin】在Android官方文档中对其语法的基本介绍和理解
- PHP 代码 微信、公众号、企业微信 发送表情符号 [U+1F449]
- 【云原生】-Docker安装部署分布式数据库 OceanBase
- be fond of the new and tired of the old? Why do it companies prefer to spend 20K on recruiting rather than raise salaries to retain old employees
- [today in history] May 29: the pioneer of sharing software was born; Chromebox launched; VoodooPC founder was born
猜你喜欢

Raspberry pie - environment settings and cross compilation

【倒立摆控制】基于UKF无迹卡尔曼滤波的倒立摆控制simulink仿真
![[2D code image correction and enhancement] simulation of 2D code image correction and enhancement processing based on MATLAB](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[2D code image correction and enhancement] simulation of 2D code image correction and enhancement processing based on MATLAB

How does win11 add printers and scanners? Win11 add printer and scanner settings

英特尔锐炫A380显卡即将在中国面市

Arduino Esp8266 Web LED控制

在线JSON转PlainText工具

Initial linear regression

PSM summary

数据清洗工具flashtext,效率直接提升了几十倍数
随机推荐
MFC CString互转LPVOID
[plug in -statistical] statistics the number of code lines and related data
[today in history] June 17: the creator of the term "hypertext" was born; The birth of Novell's chief scientist; Discovery channel on
把腾讯搬上云:云服务器 CVM 的半部进化史
Gateway微服务路由使微服务静态资源加载失败
【云原生】-Docker安装部署分布式数据库 OceanBase
数据清洗工具flashtext,效率直接提升了几十倍数
Design e-commerce seckill system
树莓派-环境设置和交叉编译
Cloud native (30) | kubernetes' app store Helm
Why are so many people keen on big factories because of the great pressure and competition?
be fond of the new and tired of the old? Why do it companies prefer to spend 20K on recruiting rather than raise salaries to retain old employees
2021年软件测试工具总结——模糊测试工具
被通知裁员后拿到5个offer
横向滚动的RecycleView一屏显示五个半,低于五个平均分布
[2D code image correction and enhancement] simulation of 2D code image correction and enhancement processing based on MATLAB
Get 5 offers after being notified of layoffs
[today in history] June 20: the father of MP3 was born; Fujitsu was established; Google acquires dropcam
PSM summary
isEmpty 和 isBlank 的用法区别