当前位置:网站首页>MySql——CRUD
MySql——CRUD
2022-07-06 00:00:00 【lion tow】
Catalog
Two 、 Filtering and sorting data
One 、 Basic query
1、 grammar :select Query list from Table name
2、 characteristic :
①、 The query list can be : Fields in the table 、 Constant values 、 expression 、 function
②、 The result of the query is a virtual table
3、 Query statement :
3.1 A single field in a query table :
SELECT last_name FROM t_mysql_employees;
3.2: Multiple fields in the query table :
SELECT last_name,salary,email FROM t_mysql_employees;
3.3: Query all fields in the table :( Enterprise multi-purpose method 1 , Because efficiency is faster )
Mode one :
SELECT
`employee_id`,
`first_name`,
`last_name`,
`phone_number`,
`last_name`,
`job_id`,
`phone_number`,
`job_id`,
`salary`,
`commission_pct`,
`manager_id`,
`department_id`,
`hiredate`
FROM
t_mysql_employees ;
Mode two :
SELECT * FROM t_mysql_employees;
3.4: Query constant value :
SELECT 100;
SELECT 'john';
result :
3.5: Query constant value :
SELECT 100%98;
result :
3.6: Query function :
SELECT VERSION();
result :
3.7: Alias purpose :
① Easy to understand
② If the fields to be queried have duplicate names , Use aliases to distinguish
3.7.1: Mode one : Use as:
SELECT 100%98 AS result ;
SELECT salary AS "out put" FROM t_mysql_employees;
Running results :
3.7.2: Mode two : Use spaces :
SELECT last_name surname ,first_name name FROM t_mysql_employees;
3.8: duplicate removal : Remove the duplicate fields in the table
SELECT DISTINCT department_id FROM t_mysql_employees;
3.9:+ The role of No : And JAVA Medium + Contrast
java Medium + Number :
① Operator , Both operands are numeric
② Connector , As long as one of the operands is a string
mysql Medium + Number : Only one function : Operator
3.9.1+ Several situations of No :
1、 Both operands are numeric , Then add :
select 100+90;
Running results :
2、 As long as one of them is character type , Convert a character numeric value to a numeric value , If the conversion is successful , Then continue to do the addition :
select '123'+90
result :
3、 If the conversion fails , Then convert the character value to 0:
select 'john'+90;
result :
4、 As long as one of them is null, The result must be null:
select null+10;
result :
Two 、 Filtering and sorting data
1、 Filter by conditional expression :
SELECT * FROM t_mysql_employees WHERE salary>12000;
2、 Filter by logical expression :
Case study 1: Query salary z stay 10000 To 20000 Between the employee's name 、 Salary and bonus
SELECT last_name, salary, commission_pct FROM t_mysql_employees WHERE salary>=10000 AND salary<=20000;
3、 Fuzzy query :
3.1、like:
Case study 2: Query employee name contains characters a Employee information
select * from employees where last_name like '%a%';
3.2、between and:
Case study 3: Query the employee number in 100 To 120 Employee information between
SELECT * FROM t_mysql_employees WHERE employee_id <= 120 AND employee_id>=100;
Compare the :
SELECT
*
FROM
t_mysql_employees
WHERE
employee_id BETWEEN 100 AND 120;
3.3、in: meaning : Determine whether the value of a field belongs to in An item in the list
Case study 4: The job number of the employee is IT_PROG、AD_VP、AD_PRES An employee's name and job number in the
SELECT
last_name,
job_id
FROM
t_mysql_employees
WHERE
job_id = 'IT_PROT' OR job_id = 'AD_VP' OR JOB_ID ='AD_PRES'
Compare the :
SELECT
last_name,
job_id
FROM
t_mysql_employees
WHERE
job_id IN( 'IT_PROT' ,'AD_VP','AD_PRES');
3.4、is null is not null:
Case study 5: Query the employee name and bonus rate without bonus ; Inquire about the employee name and bonus rate
SELECT
last_name,
commission_pct
FROM
t_mysql_employees
WHERE
commission_pct IS NULL;
SELECT
last_name,
commission_pct
FROM
t_mysql_employees
WHERE
commission_pct IS NOT NULL;
3.6 Safety is equal to <=>
Case study 6: Query the employee name and bonus rate without bonus
SELECT
last_name,
commission_pct
FROM
t_mysql_employees
WHERE
commission_pct <=>NULL;
is null And safety is the difference :
IS NULL: Can only judge NULL value , High readability , It is recommended to use
<=> : You can judge NULL value , You can also judge ordinary values , Less readable
4、order by Clause : Sort query
grammar :select Query list from Table name 【where filter 】order by Sorted fields or expressions ;
characteristic :
1、asc It's in ascending order , It can be omitted desc It's in descending order
2、order by Clause can support Single field 、 Alias 、 expression 、 function 、 Multiple fields
3、order by Clause at the end of the query statement , except limit Clause
4.1 Sort by a single field :
SELECT * FROM t_mysql_employees ORDER BY salary DESC;
4.2 Add filter criteria and sort :
Case study 7: Check department number >=90 Employee information , And in descending order of employee number
SELECT * FROM t_mysql_employees WHERE department_id>=90 ORDER BY employee_id DESC;
4.3 Sort by expression :
SELECT *,salary*12*(1+IFNULL(commission_pct,0)) FROM t_mysql_employees ORDER BY salary*12*(1+IFNULL(commission_pct,0)) DESC;
4.4 Sort by expression :
Case study 8: Query employee information In ascending order of annual salary
SELECT *,salary*12*(1+IFNULL(commission_pct,0)) Annual salary FROM t_mysql_employees ORDER BY Annual salary ASC;
4.5 Sort by function :
Case study 9: Query employee name , And in descending order of the length of the names
SELECT LENGTH(last_name),last_name FROM t_mysql_employees ORDER BY LENGTH(last_name) DESC;
4.6 Sort by function :
Case study 10: Query employee information , It is required that the salary be in descending order first , Press again employee_id Ascending
SELECT * FROM t_mysql_employees ORDER BY salary DESC,employee_id ASC;
3、 ... and 、 Group query
function : For statistical use , Also known as aggregate function or statistical function or group function
classification : sum Sum up 、avg Average 、max Maximum 、min minimum value 、count Calculate the number of
characteristic :1、sum、avg Generally used to deal with numerical type max、min、count Can handle any type of
2、 All the above grouping functions are ignored null value
3、 You can talk to distinct Match to achieve the operation of de duplication
4、count The separate introduction of the function generally uses count(*) Used to count lines
5、 The field requirement to query with grouping function is group by Fields after
1、 Simple use :
SELECT SUM(salary) FROM t_mysql_employees;
SELECT AVG(salary) FROM t_mysql_employees;
SELECT MIN(salary) FROM t_mysql_employees;
SELECT MAX(salary) FROM t_mysql_employees;
SELECT COUNT(salary) FROM t_mysql_employees;
2、 Which types of parameters are supported :
SELECT SUM(last_name) ,AVG(last_name) FROM t_mysql_employees;
SELECT SUM(hiredate) ,AVG(hiredate) FROM t_mysql_employees;
SELECT MAX(last_name),MIN(last_name) FROM t_mysql_employees;
SELECT MAX(hiredate),MIN(hiredate) FROM t_mysql_employees;
SELECT COUNT(commission_pct) FROM t_mysql_employees;
SELECT COUNT(last_name) FROM t_mysql_employees;
3、 Whether to ignore null:
SELECT SUM(commission_pct) ,AVG(commission_pct),SUM(commission_pct)/35,SUM(commission_pct)/107 FROM t_mysql_employees;
SELECT MAX(commission_pct) ,MIN(commission_pct) FROM t_mysql_employees;
SELECT COUNT(commission_pct) FROM t_mysql_employees;
SELECT commission_pct FROM t_mysql_employees;
4、 and distinct collocation :
SELECT SUM(DISTINCT salary),SUM(salary) FROM t_mysql_employees;
SELECT COUNT(DISTINCT salary),COUNT(salary) FROM t_mysql_employees;
5、count Function details :
SELECT COUNT(salary) FROM t_mysql_employees;
SELECT COUNT(*) FROM t_mysql_employees;
SELECT COUNT(1) FROM t_mysql_employees;
6、 The fields queried by grouping functions are limited employee_id It's the smallest one :
SELECT AVG(salary),employee_id FROM t_mysql_employees;
Final case analysis :
One 、 Table structure requirements :
-- 1. Student list -t_student
-- sid Student number ,sname The student's name ,sage Student age ,ssex Student gender
-- 2. Teachers list -t_teacher
-- tid Teacher number ,tname Teacher's name
-- 3. The curriculum -t_course
-- cid Course number ,cname Course name ,tid Teacher's name
-- 4. League tables -t_score
-- sid Student number ,cid Course number ,score achievement
-- Student list
insert into t_student values('01' , ' Zhao Lei ' , '1990-01-01' , ' male ');
insert into t_student values('02' , ' Qian Dian ' , '1990-12-21' , ' male ');
insert into t_student values('03' , ' Sun Feng ' , '1990-12-20' , ' male ');
insert into t_student values('04' , ' Li Yun ' , '1990-12-06' , ' male ');
insert into t_student values('05' , ' Zhou Mei ' , '1991-12-01' , ' Woman ');
insert into t_student values('06' , ' Wu Lan ' , '1992-01-01' , ' Woman ');
insert into t_student values('07' , ' Zheng Zhu ' , '1989-01-01' , ' Woman ');
insert into t_student values('09' , ' Zhang San ' , '2017-12-20' , ' Woman ');
insert into t_student values('10' , ' Li Si ' , '2017-12-25' , ' Woman ');
insert into t_student values('11' , ' Li Si ' , '2012-06-06' , ' Woman ');
insert into t_student values('12' , ' Zhao Liu ' , '2013-06-13' , ' Woman ');
insert into t_student values('13' , ' Sun Qi ' , '2014-06-01' , ' Woman ');
-- Teachers list
insert into t_teacher values('01' , ' Zhang San ');
insert into t_teacher values('02' , ' Li Si ');
insert into t_teacher values('03' , ' Wang Wu ');
-- The curriculum
insert into t_course values('01' , ' Chinese language and literature ' , '02');
insert into t_course values('02' , ' mathematics ' , '01');
insert into t_course values('03' , ' English ' , '03');
-- League tables
insert into t_score values('01' , '01' , 80);
insert into t_score values('01' , '02' , 90);
insert into t_score values('01' , '03' , 99);
insert into t_score values('02' , '01' , 70);
insert into t_score values('02' , '02' , 60);
insert into t_score values('02' , '03' , 80);
insert into t_score values('03' , '01' , 80);
insert into t_score values('03' , '02' , 80);
insert into t_score values('03' , '03' , 80);
insert into t_score values('04' , '01' , 50);
insert into t_score values('04' , '02' , 30);
insert into t_score values('04' , '03' , 20);
insert into t_score values('05' , '01' , 76);
insert into t_score values('05' , '02' , 87);
insert into t_score values('06' , '01' , 31);
insert into t_score values('06' , '03' , 34);
insert into t_score values('07' , '02' , 89);
insert into t_score values('07' , '03' , 98);
Two 、 subject :
01) Inquire about " 01 " Course than " 02 " Information and course marks for students with high course grades
select a.*,b.score,c.score from t_student a,t_score b,t_score c where a.sid=b.sid and a.sid=c.sid and b.cid='01' and c.cid='02' and b.score > c.score
02) Queries exist at the same time " 01 " Courses and " 02 " Course situation
select a.*,b.cid,c.cid from t_student a,t_score b,t_score c where a.sid=b.sid and a.sid=c.sid and b.cid='01' and c.cid='02'
03) Query exists " 01 " The course may not exist " 02 " Course situation ( If it does not exist, it will be displayed as null )
select a.*,b.cid,c.cid from t_student a,t_score b,t_score c where a.sid=b.sid and a.sid=c.sid and b.cid='01' and c.cid not in('02')
04) Query does not exist " 01 " Course but there is " 02 " Course situation
select a.*,b.cid,c.cid from t_student a,t_score b,t_score c where a.sid=b.sid and a.sid=c.sid and c.cid not in('01') and b.cid='02'
05) Query average score is greater than or equal to 60 Student number, student name and average score of each student
select a.sid,a.sname,AVG(b.score) from t_student a,t_score b where a.sid = b.sid GROUP BY b.sid HAVING AVG(b.score)>=60
06) The query in t_score There is student information about the scores in the table
select * from t_student where sid not in(select DISTINCT(sid) from t_score)
07) Query student numbers of all students 、 The student's name 、 The total number of selected courses 、 Total grade of all courses ( No results are shown as null )
select a.sid,a.sname,COUNT(b.cid),SUM(b.score) from t_student a,t_score b where a.sid = b.sid GROUP BY b.sid
08) Inquire about 「 Li 」 The number of teachers surnamed
select count(*) from t_teacher where tname like ' Li %'
09) I have learned to query 「 Zhang San 」 Information of students taught by teachers
select a.*,c.tname from t_student a , t_score b ,t_teacher c ,t_course d where a.sid=b.sid and b.cid = d.cid and d.tid = c.tid and b.cid='01'
10) Check the information of students who have not learned all the courses
select * from t_student where sid not in(select a.sid from t_student a,t_score b,t_score c,t_score d where a.sid=b.sid and a.sid=c.sid and b.cid='01' and c.cid='02' and d.cid='03')
11) I didn't learn how to query " Zhang San " The name of the student in any course taught by the teacher
select sname from t_student where sname not in(select a.sname from t_student a , t_score b ,t_teacher c ,t_course d where a.sid=b.sid and b.cid = d.cid and d.tid = c.tid and b.cid='01')
12) Check the student number of two or more failed courses , Name and average score
select * from t_student a,t_score b where a.sid=b.sid and a.sid in(select c.sid from t_score c where c.score<60 GROUP BY c.sid HAVING COUNT(*)>1)
13) retrieval " 01 " The course score is less than 60, Student information in descending order of scores
select a.* from t_student a,t_score b where a.sid=b.sid and b.cid='01' and b.score<60 ORDER BY b.score desc
14) Show the grades of all courses and the average grades of all students from high to low
select a.*,AVG(b.score) from t_student a,t_score b where a.sid = b.sid GROUP BY b.sid HAVING AVG(b.score)>=0 ORDER BY AVG(b.score) desc
15) Check the highest scores of all subjects 、 Minimum and average points :
Show as follows : Course ID, Course name, The highest , Lowest score , average , pass rate , Medium rate , Excellent rate , The pass rate of excellence is >=60, The average is :70-80, Good is :80-90, Excellence is :>=90
Require output of course number and number of electives , The query results are arranged in descending order of the number of people , If the number of people is the same , In ascending order of course number
select a.cid,cname,max(a.score)' The highest ',min(a.score)' Lowest score ',avg(a.score)' average ',((select count(sid) from t_score where score>=60 and cid=b.cid )/(select count(sid) from t_score where cid=b.cid)) ' pass rate ' from t_score a
inner join t_course b on a.cid = b.cid
group by b.cid;
边栏推荐
- [day39 literature extensive reading] a Bayesian perspective on magnetic estimation
- 【luogu CF487E】Tourists(圆方树)(树链剖分)(线段树)
- Online yaml to CSV tool
- Redis high availability - master-slave replication, sentinel mode, cluster
- 什么叫做信息安全?包含哪些内容?与网络安全有什么区别?
- China Jinmao online electronic signature, accelerating the digitization of real estate business
- 【LeetCode】5. Valid palindrome
- Bao Yan notebook IV software engineering and calculation volume II (Chapter 8-12)
- [Luogu p3295] mengmengda (parallel search) (double)
- 云呐|固定资产管理系统功能包括哪些?
猜你喜欢
用列錶初始化你的vector&&initializer_list簡介
Problems encountered in the database
软件测试工程师必会的银行存款业务,你了解多少?
Initialize your vector & initializer with a list_ List introduction
时区的区别及go语言的time库
认识提取与显示梅尔谱图的小实验(观察不同y_axis和x_axis的区别)
Hardware and interface learning summary
China Jinmao online electronic signature, accelerating the digitization of real estate business
云呐|固定资产管理系统功能包括哪些?
Mathematical model Lotka Volterra
随机推荐
PV静态创建和动态创建
What if the C disk is not enough? Let's see how I can clean up 25g of temp disk space after I haven't redone the system for 4 years?
云呐|固定资产管理系统主要操作流程有哪些
Determinant learning notes (I)
上门预约服务类的App功能详解
MySQL之函数
[Luogu p3295] mengmengda (parallel search) (double)
2022.7.5-----leetcode.729
Single merchant v4.4 has the same original intention and strength!
CloudCompare&PCL 点云随机添加噪声
Mathematical model Lotka Volterra
总结了 800多个 Kubectl 别名,再也不怕记不住命令了!
What is a humble but profitable sideline?
After summarizing more than 800 kubectl aliases, I'm no longer afraid that I can't remember commands!
MySql——CRUD
Zhuan: in the future, such an organization can withstand the risks
XML configuration file (DTD detailed explanation)
Wechat applet -- wxml template syntax (with notes)
云呐|固定资产管理系统功能包括哪些?
【luogu CF487E】Tourists(圆方树)(树链剖分)(线段树)