当前位置:网站首页>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();
边栏推荐
- CMU提出NLP新范式—重构预训练,高考英语交出134高分
- Initial linear regression
- 【Kotlin】在Android官方文档中对其语法的基本介绍和理解
- 第三批入围企业公示!年度TOP100智能网联供应商评选
- [today in history] May 31: the father of Amiga was born; The co developer of basic language was born; BlackBerry BBM shutdown
- 【方块编码】基于matlab的图像方块编码仿真
- How fiddle uses agents
- 分布式事务—基于消息补偿的最终一致性方案(本地消息表、消息队列)
- isEmpty 和 isBlank 的用法区别
- JDBC与MySQL数据库
猜你喜欢

分布式事务解决方案Seata-Golang浅析

> Could not create task ‘:app:MyTest. main()‘. > SourceSet with name ‘main‘ not found. Problem repair

微信小程序中生成二维码

数据清洗工具flashtext,效率直接提升了几十倍数

迪赛智慧数——柱状图(折柱混合图):2021年毕业季租房价格和房租收入比

JDBC and MySQL databases

Win11不能拖拽图片到任务栏软件上快速打开怎么办

榜单首发——前装搭载率站上10%大关,数字钥匙方案供应商TOP10

【方块编码】基于matlab的图像方块编码仿真
![[today in history] June 25: the father of notebook was born; Windows 98 release; First commercial use of generic product code](/img/ef/a26127284fe57ac049a4313d89cf97.png)
[today in history] June 25: the father of notebook was born; Windows 98 release; First commercial use of generic product code
随机推荐
Intel Ruixuan A380 graphics card will be launched in China
PSM总结
Summary of software testing tools in 2021 - fuzzy testing tools
MFC CString to LPVOID
Shardingsphere-proxy-5.0.0 establish MySQL read / write separation connection (6)
Packet capturing and sorting out external Fiddler -- understanding the toolbar [1]
LiveData 面试题库、解答---LiveData 面试 7 连问~
Writing based on stm32
[today in history] June 5: Lovelace and Babbage met; The pioneer of public key cryptography was born; Functional language design pioneer born
> Could not create task ‘:app:MyTest.main()‘. > SourceSet with name ‘main‘ not found.问题修复
Why are so many people keen on big factories because of the great pressure and competition?
MFC常用 当前路径
被通知裁员后拿到5个offer
为什么大厂压力大,竞争大,还有这么多人热衷于大厂呢?
How does win11 close recently opened projects? Win11 method to close recently opened projects
adb双击POWER键指令
Step by step interpretation of crf+bilstm code
[today in history] June 16: Oracle Bone Inscriptions was established; Microsoft MSX was born; The inventor of fast Fourier transform was born
[today in history] June 1: Napster was founded; MS-DOS original author was born; Google sells Google SketchUp
Win11不能拖拽图片到任务栏软件上快速打开怎么办