当前位置:网站首页>Common MySQL database functions and queries
Common MySQL database functions and queries
2022-06-25 23:02:00 【Forestᝰ】
1. Aggregate functions
Use aggregate function to facilitate data statistics
Aggregate function cannot be in where Use in
Common aggregate functions
count(): Total number of records queried
max(): Query maximum
min(): Query minimum
sum(): Sum up
avg(): averaging
1.1 Total number of records queried
count() To calculate the total number of lines , Field names can also be used in parentheses
Example : Check the total number of students
select count() from students;
1.2 Query maximum
max( Column ) To find the maximum value of this column
Example : Query the maximum age of girls
select max(age) from students where sex=‘ Woman ’;
1.3 Query minimum
min( Column ) To find the minimum value of this column
Example : Inquire about 1 The minimum age of the class
select min(age) from students;
1.4 Sum up
sum( Column ) Means to find the sum of this column
Example : Check the total age of students in Beijing
select sum(age) from students where hometown=‘ Beijing ’;
1.5 averaging
avg( Column ) Means to find the average value of this column
Example : Find out the average age of girls
select avg(age) from students where sex=‘ Woman ’
Data manipulation - Additions and deletions
1. Simple query
selectfrom Table name
example : Query all student data
selectfromstudents
2. Add data
2.1 Add a row of data
Format 1 : Set values for all fields , The order of values corresponds to the order of fields in the table
explain : The primary key column is auto grow , Space occupation is required during insertion , Usually use 0 perhaps default perhaps null Come and take the place , Insert
After success, the actual data shall prevail
insertinto Table name values(…)
example : Insert a student , Set information for all fields
insertintostudentsvalues(0,‘ Arthur ’,22,177.56)
Format two : Some fields set values , The order of the values corresponds to the order of the fields given
insertinto Table name ( Field 1,…)values( value 1,…)
example : Insert a student , Just set the name
insertintostudents(name)values(‘ The professor ’)
2.2 Add multiple rows of data
Mode one : Write multiple insert sentence , Sentences are separated by semicolons
insertintostudents(name)values(‘ The professor 2’);
insertintostudents(name)values(‘ The professor 3’);
insertintostudentsvalues(0,‘ Arthur 2’,23,167.56)
Mode two : Write a insert sentence , Set multiple pieces of data , The data are separated by English commas
Format 1 :insertinto Table name values(…),(…)…
example : Insert multiple students , Set information for all fields
insertintostudentsvalues(0,‘ Arthur 3’,23,167.56),(0,‘ Arthur 4’,23,167.56)
Format two :insertinto Table name ( Column 1,…)values( value 1,…),( value 1,…)…
example : Insert multiple students , Just set the name
insertintostudents(name)values(‘ The professor 5’),(‘ The professor 6’)
3. modify
update Table name set Column 1= value 1, Column 2= value 2…where Conditions
example : modify id by 5 Student data for , Name changed to di Renjie , Change the age to 20
updatestudentssetname=‘ Judge dee ’,age=20whereid=5
4. Delete
Format 1 :deletefrom Table name where Conditions
example : Delete id by 6 Student data for
deletefromstudentswhereid=6
Logical deletion : For important data , Cannot be easily executed delete Statement to delete . Because once deleted , Data cannot be recovered
complex , At this time, you can delete it logically .
1、 Add fields to the table , Represents whether the data is deleted , Usually named isdelete,0 Represents not deleted ,1 On behalf of the delete , The default value is 0
2、 When you want to delete a piece of data , You only need to set the of this data isdelete Field is 1
3、 When querying data in the future , Just find out isdelete by 0 The data of
example :
1、 Add fields to the student table (isdelete), The default value is 0,
If the table already has data , Need to put all the data isdelete The field is updated to 0
updatestudentssetisdelete=0
2、 Delete id by 1 Of the students
updatestudentssetisdelete=1whereid=1
3、 Query undeleted data
selectfromstudentswhereisdelete=0
Format two :truncatetable Table name ( Delete all data from the table , Keep the table structure )
example : Delete all data in the student table
truncatetablestudents
Format three :droptable Table name ( Delete table , Delete all data and table structures )
example : Delete student table
droptablestudents
Truncate、Delete、Drop The difference between
1、Delete When deleting data , Even if all data is deleted , The self growth field will not change from 1 Start
2、Truncate When deleting data , The self growth field is recovered from 1 Start
3、Drop Is to delete the table , Delete all data and table structures
summary
In speed ,drop>truncate>delete
If you want to delete some data, use delete, Pay attention to take where Clause
If you want to delete a table , use drop
If you want to keep the table and delete all the data , The self growth field is restored from 1 Start , use truncate
Data manipulation - Inquire about
1. Data preparation
1.1 Create data table
droptableifexistsstudents;
createtablestudents(
studentNovarchar(10)primarykey,
namevarchar(10),
sexvarchar(1),
hometownvarchar(20),
agetinyint(4),
classvarchar(10),
cardvarchar(20)
);
1.2 insert data
insertintostudentsvalues
(‘001’,‘ Wang Zhaojun ’,‘ Woman ’,‘ Beijing ’,‘20’,‘1 class ’,‘340322199001247654’),
(‘002’,‘ Zhugeliang ’,‘ male ’,‘ Shanghai ’,‘18’,‘2 class ’,‘340322199002242354’),
(‘003’,‘ Zhang Fei ’,‘ male ’,‘ nanjing ’,‘24’,‘3 class ’,‘340322199003247654’),
(‘004’,‘ White ’,‘ male ’,‘ anhui ’,‘22’,‘4 class ’,‘340322199005247654’),
(‘005’,‘ Big Joe ’,‘ Woman ’,‘ tianjin ’,‘19’,‘3 class ’,‘340322199004247654’),
(‘006’,‘ Sun shangxiang ’,‘ Woman ’,‘ hebei ’,‘18’,‘1 class ’,‘340322199006247654’),
(‘007’,‘ Hundred Li xuance ’,‘ male ’,‘ shanxi ’,‘20’,‘2 class ’,‘340322199007247654’),
(‘008’,‘ Little Joe ’,‘ Woman ’,‘ Henan ’,‘15’,‘3 class ’,null),
(‘009’,‘ Hundred Li keep the promise ’,‘ male ’,‘ hunan ’,‘21’,‘1 class ’,’’),
(‘010’,‘ Daji ’,‘ Woman ’,‘ guangdong ’,‘26’,‘2 class ’,‘340322199607247654’),
(‘011’,‘ Li Bai ’,‘ male ’,‘ Beijing ’,‘30’,‘4 class ’,‘340322199005267754’),
(‘012’,‘ Sun Bin ’,‘ male ’,‘ xinjiang ’,‘26’,‘3 class ’,‘340322199000297655’);
2. Query basic syntax
2.1 Query all fields
grammar :
selectfrom Table name
1.1 Common table query connection methods
Internal connection : The result of the query is the data matched by the two tables
grammar :
selectfrom surface 1
innerjoin surface 2on surface 1. Column = surface 2. Column
example 1: Inquire about students' information and their grades
select
*
from
studentsstu
innerjoinscoressconstu.studentNo=sc.studentNo
Expand : Another syntax for inner join
selectfrom surface 1, surface 2where surface 1. Column = surface 2. Column
example 1: Inquire about students' information and their grades
select
*
from
studentsstu,
scoressc
where
stu.studentNo=sc.studentNo
example 2: Query course information and course grades
select
*
from
coursescs
innerjoinscoressconcs.courseNo=sc.courseNo
example 3: Query the student information and the corresponding score of the course
select
*
from
studentsstu
innerjoinscoressconstu.studentNo=sc.studentNo
innerjoincoursescsoncs.courseNo=sc.courseNo
example 4: Inquire about Wang Zhaojun's achievements , Ask for name 、 Course no. 、 achievement
select
stu.name,
sc.courseNo,
sc.score
from
studentsstu
innerjoinscoressconstu.studentNo=sc.studentNo
where
stu.name=‘ Wang Zhaojun ’
example 5: Query Wang Zhaojun's database results , Ask for name 、 Course name 、 achievement
select
stu.name,
cs.name,
sc.score
from
studentsstuinnerjoinscoressconstu.studentNo=sc.studentNo
innerjoincoursescsonsc.courseNo=cs.courseNo
where
stu.name=' Wang Zhaojun ’andcs.name=‘ database ’
example 6: Query all students' scores in the database , Ask for name 、 Course name 、 achievement
select
stu.name,
cs.name,
sc.score
from
studentsstu
innerjoinscoressconstu.studentNo=sc.studentNo
innerjoincoursescsonsc.courseNo=cs.courseNo
where
cs.name=‘ database ’
example 7: The highest score of boys , Ask for name 、 Course name 、 achievement
select
stu.name,
cs.name,
sc.score
from
studentsstu
innerjoinscoressconstu.studentNo=sc.studentNo
innerjoincoursescsonsc.courseNo=cs.courseNo
where
stu.sex=‘ male ’
orderby
sc.scoredesc
limit1
边栏推荐
- What are the channels for Internet advertising to gain customers?
- Openwrt (VIII) application layer development
- 2022年中职组网络安全新赛题
- Fastjson反序列化随机性失败
- How do I project points on a 3D plane- How to project a point onto a plane in 3D?
- Flutter 网络请求封装之Dio(Cookie管理、添加拦截器、下载文件、异常处理、取消请求等)
- Mysql database index
- Simple and easy-to-use cache library gcache
- 荣耀推出积分商城,支持兑换各种荣耀产品
- Why is BeanUtils not recommended?
猜你喜欢

Three layer architecture + routing experiment
![[WPF] XAML code skills that can be directly used for converting CAD engineering drawings to WPF](/img/a1/09d2dc0ec47c54530da4d42d218d1c.jpg)
[WPF] XAML code skills that can be directly used for converting CAD engineering drawings to WPF

2022 love analysis · panoramic report of it operation and maintenance manufacturers

2022爱分析· IT运维厂商全景报告

ES7/ES9 -- 新特性与正则

Civil Aviation Administration: by 2025, China will initially build a safe, intelligent, efficient and green aviation logistics system

【EOSIO】EOS/WAX签名错误 is_canonical( c ): signature is not canonical 问题

Eureka core ⼼ source code analysis

Use apiccloud AVM multi terminal component to quickly realize the search function in the app
![Lecture 14 of the Blue Bridge Cup -- number theory [exercises]](/img/96/0971909c8bf25820c2d4f520bb83fb.jpg)
Lecture 14 of the Blue Bridge Cup -- number theory [exercises]
随机推荐
Flutter 網絡請求封裝之Dio(Cookie管理、添加攔截器、下載文件、异常處理、取消請求等)
Analysis report on market demand situation and investment direction of China's optical transmission equipment industry from 2022 to 2028
Privatization lightweight continuous integration deployment scheme -- 03 deployment of Web services (Part 2)
Another breakthrough! Alibaba cloud enters the Gartner cloud AI developer service Challenger quadrant
Open source optimized VVC encoder in general scenarios
Development trend of China's power carrier communication industry and Research Report on the 14th five year plan 2022 ~ 2028
NRM source switching tool
The difference between synchronize and volatile
Touring band: a 5g based multi camera remote distributed video production experiment
万亿热钱砸向太空经济,真的是一门好生意?
不荒唐的茶小程序-规则改动
Dio encapsulated by the flutter network request (cookie management, adding interceptors, downloading files, exception handling, canceling requests, etc.)
Yyds dry goods inventory JD 2, why is redis so fast?
2022-2028 global SiC igniter industry research and trend analysis report
华为云SRE确定性运维专刊(第一期)
Unity技术手册 - 粒子基础主模块属性-上
How to use the find command
Three layer architecture + routing experiment
腾讯《和平精英》新版本将至:新增账号安全保护系统,游戏内违规行为检测升级
Mysql database index