当前位置:网站首页>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
边栏推荐
- The Ping class of unity uses
- Glory launched the points mall to support the exchange of various glory products
- Travel notes of 2022giao
- APP-新功能上线
- Tlog helps Pangu framework realize microservice link log tracking
- TLog 助力盘古框架实现微服务链路日志追踪
- Reasons why MySQL cannot be connected externally after installing MySQL database on ECs and Solutions
- Programmer weekly (issue 4): the wealth view of programmers
- What do l and R of earphone mean?
- Zhihu Gaozan: what ability is important, but most people don't have it?
猜你喜欢

如何用jmeter做接口测试

Unity的Ping類使用

小程序绘制一个简单的饼图

万亿热钱砸向太空经济,真的是一门好生意?
![[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

ES6学习-- LET

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

Chapter 3 use of requests Library
[email protected]@COLLATION_CONNECTION */"/>.sql数据库导入错误:/*!40101 SET @[email protected]@COLLATION_CONNECTION */

Which PHP open source works deserve attention
随机推荐
Raspberry PI (bullseye) replacement method of Alibaba cloud source
Use apiccloud AVM multi terminal component to quickly realize the search function in the app
2022-2028 global DC linear variable differential transformer (LVDT) industry survey and trend analysis report
c语言与数据库的创建使用
How to design a complex business system? From the understanding of domain design, cloud native, micro service, and middle platform
Flutter 网络请求封装之Dio(Cookie管理、添加拦截器、下载文件、异常处理、取消请求等)
2022 love analysis · panoramic report of it operation and maintenance manufacturers
等价类,边界值,场景法的使用方法和运用场景
ES6-- 模板字符串、对象的简化写法、箭头函数
万亿热钱砸向太空经济,真的是一门好生意?
Wpewebkit debugging MSE playback
Tlog helps Pangu framework realize microservice link log tracking
提问的智慧?如何提问?
面对AI人才培养的“产学研”鸿沟,昇腾AI如何做厚产业人才黑土地?
2022-2028 global transmission type photoelectric circuit breaker industry research and trend analysis report
2022-2028 global co extrusion production line industry research and trend analysis report
App test points
Unity technical manual - particle emission and life cycle velocity sub module
adb常用命令
荣耀推出积分商城,支持兑换各种荣耀产品