当前位置:网站首页>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
边栏推荐
- Glory launched the points mall to support the exchange of various glory products
- 一位博士在华为的22年
- Touring band: a 5g based multi camera remote distributed video production experiment
- Huawei cloud SMS has tested that many mobile phones prompt frequent sending
- 关闭MongoDB一些服务需要注意的地方(以及开启的相关命令)
- Hello, teacher, is it really safe to open an account in Alipay fund?
- zabbix_ Server configuration file details
- 2022年中职组网络安全新赛题
- ES6 --- 数值扩展、对象拓展
- Talk about adapter mode
猜你喜欢

Data governance is easier said than done

Unity的Ping類使用
Interview shock 23: talk about thread life cycle and transformation process?

Which PHP open source works deserve attention
[email protected]@COLLATION_CONNECTION */"/>.sql数据库导入错误:/*!40101 SET @[email protected]@COLLATION_CONNECTION */

Use of local stack in flask

2022-2028 global TFT LCD touch screen industry research and trend analysis report

Wpewebkit debugging MSE playback

2022-2028 global carbon fiber unidirectional tape industry research and trend analysis report

不荒唐的茶小程序-规则改动
随机推荐
What do l and R of earphone mean?
Civil Aviation Administration: by 2025, China will initially build a safe, intelligent, efficient and green aviation logistics system
不荒唐的茶小程序-规则改动
2022-2028 global TFT LCD touch screen industry research and trend analysis report
2022-2028 global extrusion coating and lamination production line industry research and trend analysis report
Lecture 14 of the Blue Bridge Cup -- number theory [example]
2022-2028 global vacuum jacket system industry survey and trend analysis report
2022爱分析· IT运维厂商全景报告
多模态数据也能进行MAE?伯克利&谷歌提出M3AE,在图像和文本数据上进行MAE!最优掩蔽率可达75%,显著高于BERT的15%...
Intimacy - [comfortable exit] - final communication to reduce injury
Reasons why MySQL cannot be connected externally after installing MySQL database on ECs and Solutions
Initialization process of gstlibav
简单好用的缓存库 gcache
2022年河南省第一届职业技能大赛网络安全项目试题
Cvpr2022 tutorial | machine learning remote sensing processing: agriculture and food security, University of Maryland
Facing the "industry, University and research" gap in AI talent training, how can shengteng AI enrich the black land of industrial talents?
Global and Chinese oleic acid operation mode and market supply and demand forecast report 2022 ~ 2028
Unity技术手册 - 粒子发射和生命周期内速度子模块
2、一个向量乘它的转置,其几何意义是什么?
zabbix_ Server configuration file details