当前位置:网站首页>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();
边栏推荐
- How to run unity webgl after packaging (Firefox configuration)
- MFC常用 当前路径
- [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 12: the United States entered the era of digital television; Mozilla's original developer was born; 3com merges with American Robotics
- Shuttle uses custompaint to paint basic shapes
- [today in history] June 20: the father of MP3 was born; Fujitsu was established; Google acquires dropcam
- Get 5 offers after being notified of layoffs
- Is it reliable to invest in the inter-bank certificate of deposit fund? Is the inter-bank certificate of deposit fund safe
- 【Kotlin】在Android官方文档中对其语法的基本介绍和理解
- isEmpty 和 isBlank 的用法區別
猜你喜欢

Writing based on stm32

3年功能测试拿8K,被刚来的测试员反超,其实你在假装努力
![[today in history] June 13: parent-child disputes in packet switched networks; The founder of game theory was born; The embryonic form of interactive television](/img/2c/01e3be3c5b4f8e6a7853547ffd1bbd.png)
[today in history] June 13: parent-child disputes in packet switched networks; The founder of game theory was born; The embryonic form of interactive television

Severe Tire Damage:世界上第一个在互联网上直播的摇滚乐队

【插件-statistic】统计代码行数和相关数据
![[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

Cloud native (30) | kubernetes' app store Helm

Stm32f1 and stm32subeide programming example - metal touch sensor driver

第一次使用gcc和makefile编写c程序
![[today in history] June 19: iPhone 3GS launched; Pascal was born; Anti terrorist elite begins testing](/img/ec/90961351a0de1eac26dd003bf25d34.png)
[today in history] June 19: iPhone 3GS launched; Pascal was born; Anti terrorist elite begins testing
随机推荐
What if win11 cannot use dynamic wallpaper? Solution of win11 without dynamic wallpaper
The graduation season is coming, and the number of college graduates in 2022 has exceeded 10 million for the first time
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
毕业季来临,2022届高校毕业生人数首次突破千万大关
StaticLayout的使用详解
2021年软件测试工具总结——模糊测试工具
math_(函数&数列)极限的含义&误区和符号梳理/邻域&去心邻域&邻域半径
【 amélioration de la correction d'image de Code bidimensionnel】 simulation du traitement d'amélioration de la correction d'image de Code bidimensionnel basée sur MATLAB
LiveData 面试题库、解答---LiveData 面试 7 连问~
【方块编码】基于matlab的图像方块编码仿真
横向滚动的RecycleView一屏显示五个半,低于五个平均分布
[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 10: Apple II came out; Microsoft acquires gecad; The scientific and technological pioneer who invented the word "software engineering" was born
Packet capturing and sorting out external Fiddler -- understanding the toolbar [1]
SQL reported an unusual error, which confused the new interns
win11如何添加打印机和扫描仪?win11添加打印机和扫描仪的设置
迪赛智慧数——柱状图(折柱混合图):2021年毕业季租房价格和房租收入比
如何开启多语言文本建议?Win11打开多语言文本建议的方法
isEmpty 和 isBlank 的用法区别
[today in history] June 18: JD was born; The online store platform Etsy was established; Facebook releases Libra white paper