当前位置:网站首页>Crud of MySQL
Crud of MySQL
2022-07-05 14:49:00 【An Li Jiu Ge】
Catalog
Preface
Last time I shared MySQL Account management , What we want to share this time is CRUD, Anyway CRUD There must be some .
One 、 The basic query
#1. A single field in a query table
SELECT department_id FROM t_mysql_employees
#2. Multiple fields in the query table
SELECT department_id,employee_id FROM t_mysql_employees
#3. Query all fields in the table
SELECT * FROM t_mysql_employees
#4. Query constant value
SELECT 100;
SELECT 'john';
#5. Query expression
SELECT 100%98;
#6. Query function
SELECT VERSION();
#7. names
SELECT department_id Department number FROM t_mysql_employees
#8. duplicate removal
SELECT DISTINCT department_id FROM t_mysql_employees
# Case study : Query all department numbers involved in the employee table
SELECT DISTINCT department_id FROM t_mysql_employees
#9.+ The role of No
SELECT 'join'+1
SELECT '100'+1Running results :
#1. A single field in a query table
#2. Multiple fields in the query table
#3. Query all fields in the table
#4. Query constant value
SELECT 100;
SELECT 'john';
#5. Query expression
SELECT 100%98;
#6. Query function
SELECT VERSION();
#7. names
#8. duplicate removal
#9.+ The role of No
Two 、 Filter query
# One 、 Filter by conditional expression
# Case study 1: Query salary >12000 Employee information
SELECT * FROM t_mysql_employees WHERE salary > 12000
# Case study 2: Query department number is not equal to 90 The employee name and department number of the
SELECT first_name,department_id FROM t_mysql_employees WHERE NOT(department_id=90)
# Two 、 Filter by logical expression
# Case study 1: Query salary z stay 10000 To 20000 Between the employee's name 、 Salary and bonus
SELECT first_name,salary,commission_pct FROM t_mysql_employees WHERE salary BETWEEN 10000 AND 20000
# Case study 2: The inquiry department number is not in 90 To 110 Between , Or pay more than 15000 Employee information
SELECT * FROM t_mysql_employees WHERE NOT(department_id BETWEEN 90 AND 110) or salary > 12000
# 3、 ... and 、 Fuzzy query
#1.like
# Case study 1: Query employee name contains characters a Employee information
SELECT * FROM t_mysql_employees WHERE first_name LIKE '%a%'
# Case study 2: The third character in the query employee name is e, The fifth character is a The name and salary of the employee
SELECT * FROM t_mysql_employees WHERE first_name LIKE '__e_a%'
# Case study 3: The second character in the employee name is _ Employees
SELECT * FROM t_mysql_employees WHERE last_name LIKE
#2.between and
# Case study 1: Query the employee number in 100 To 120 Employee information between
SELECT * FROM t_mysql_employees WHERE employee_id BETWEEN 100 AND 120
#3.in
# Case study : The job number of the employee is IT_PROG、AD_VP、AD_PRES An employee's name and job number in the
SELECT first_name,job_id FROM t_mysql_employees WHERE job_id IN ('IT_PROG','AD_VP','AD_PRES')
#4、is null
# Case study 1: Query the employee name and bonus rate without bonus
SELECT first_name,commission_pct FROM t_mysql_employees WHERE commission_pct is null
# Case study 2: Check the name and bonus rate of employees with bonus
SELECT first_name,commission_pct FROM t_mysql_employees WHERE not commission_pct is null
# Safety is equal to <=>
# Case study 1: Query the employee name and bonus rate without bonus
SELECT first_name,commission_pct FROM t_mysql_employees WHERE commission_pct <=> null
# Case study 2: Query salary as 12000 Employee information
SELECT * FROM t_mysql_employees WHERE salary <=>12000
#is null pk <=>
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 One 、 Filter by conditional expression
# Case study 1: Query salary >12000 Employee information
# Case study 2: Query department number is not equal to 90 The employee name and department number of the
# Two 、 Filter by logical expression
# Case study 1: Query salary z stay 10000 To 20000 Between the employee's name 、 Salary and bonus
# Case study 2: The inquiry department number is not in 90 To 110 Between , Or pay more than 15000 Employee information
# 3、 ... and 、 Fuzzy query
#1.like
# Case study 1: Query employee name contains characters a Employee information
# Case study 2: The third character in the query employee name is e, The fifth character is a The name and salary of the employee
#2.between and
# Case study 1: Query the employee number in 100 To 120 Employee information between
#3.in
# Case study : The job number of the employee is IT_PROG、AD_VP、AD_PRES An employee's name and job number in the
![]()
#4、is null
# Case study 1: Query the employee name and bonus rate without bonus
# Case study 2: Check the name and bonus rate of employees with bonus
# Safety is equal to <=>
# Case study 1: Query the employee name and bonus rate without bonus
# Case study 2: Query salary as 12000 Employee information
#is null pk <=>
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
3、 ... and 、 Sort
order by Clause
#1、 Sort by a single field
# Case study : Sort by employee table salary
SELECT * FROM t_mysql_employees ORDER BY salary
#2、 Add filter criteria and sort
# Case study : 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
#3、 Sort by expression
# Case study : Query employee information In descending order of annual salary
SELECT em.salary*12*(1+IFNULL(commission_pct,0)),em.* FROM t_mysql_employees em ORDER BY em.salary*12*(1+IFNULL(commission_pct,0)) DESC
#4、 Sort by alias
# Case study : Query employee information In ascending order of annual salary
SELECT em.salary*12*(1+IFNULL(commission_pct,0)),em.* FROM t_mysql_employees em ORDER BY em.salary*12*(1+IFNULL(commission_pct,0)) ASC
#5、 Sort by function
# Case study : Query employee name , And in descending order of the length of the names
SELECT first_name FROM t_mysql_employees ORDER BY LENGTH(first_name) DESC
#6、 Sort by multiple fields
# Case study : 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
#1. Check the employee's name, department number and annual salary , In descending order of annual salary Ascending by name
SELECT em.first_name,em.department_id,em.salary*12*(1+IFNULL(commission_pct,0)) FROM t_mysql_employees em ORDER BY em.salary*12(1+IFNULL(commission_pct,0)) DESC,first_name asc
#2. The choice of salary is not in 8000 To 17000 The name and salary of the employee , In descending order of wages
SELECT first_name,salary FROM t_mysql_employees WHERE NOT(salary BETWEEN 8000 AND 17000) ORDER BY salary desc
#3. Query mailbox contains e Employee information , And first by the number of bytes in the mailbox , And then in ascending order by department number
SELECT * FROM t_mysql_employees WHERE email LIKE '%e%' ORDER BY LENGTH(email)DESC,department_id ASCorder by Clause
#1、 Sort by a single field
# Case study : Sort by employee table salary
#2、 Add filter criteria and sort
# Case study : Check department number >=90 Employee information , And in descending order of employee number
#3、 Sort by expression
# Case study : Query employee information In descending order of annual salary
#4、 Sort by alias
# Case study : Query employee information In ascending order of annual salary
#5、 Sort by function
# Case study : Query employee name , And in descending order of the length of the names
#6、 Sort by multiple fields
# Case study : Query employee information , It is required that the salary be in descending order first , Press again employee_id Ascending
#1. Check the employee's name, department number and annual salary , In descending order of annual salary Ascending by name
#2. The choice of salary is not in 8000 To 17000 The name and salary of the employee , In descending order of wages
#3. Query mailbox contains e Employee information , And first by the number of bytes in the mailbox , And then in ascending order by department number
Four 、 Case study
·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);
#01) Inquire about " 01 " Course than " 02" Information and course marks for students with high course grades
SELECT st.*,sc.score AS '01',sc2.score AS '02' FROM t_student st
LEFT JOIN t_score sc ON sc.sid=st.sid AND sc.cid='01'
LEFT JOIN t_score sc2 ON sc2.sid=st.sid AND sc2.cid='02'
WHERE sc.score>sc2.score
#02) Queries exist at the same time " 01 " Courses and " 02 " Course situation
SELECT st.* FROM t_student st INNER JOIN t_score sc ON sc.sid=st.sid AND sc.cid='01'
WHERE st.sid IN (
SELECT st2.sid FROM t_student st2 INNER JOIN t_score sc2 ON sc2.sid=st2.sid AND sc2.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 st.* FROM t_student st INNER JOIN t_score sc ON sc.sid=st.sid AND sc.cid='01'
WHERE st.sid NOT IN (
SELECT st2.sid FROM t_student st2 INNER JOIN t_score sc2 ON sc2.sid=st2.sid AND sc2.cid='02'
)
#04) Query does not exist " 01 " Course but there is " 02 " Course situation
SELECT * FROM t_student st INNER JOIN t_score sc ON sc.sid=st.sid AND sc.cid='02'
WHERE st.sid NOT IN (
SELECT st2.sid FROM t_student st2 INNER JOIN t_score sc2 ON sc2.sid=st2.sid AND sc2.cid='01'
)
#05) Query average score is greater than or equal to 60 Student number, student name and average score of each student
SELECT st.sid,st.sname,ROUND(AVG(sc.score),2) average
FROM t_student st
LEFT JOIN t_score sc ON st.sid=sc.sid
GROUP BY st.sid HAVING AVG(sc.score)>=60;
#06) The query in t_score There is student information about the scores in the table
SELECT st.* FROM t_student st WHERE sid IN(
SELECT sc.sid FROM t_score sc
)
#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 st.sid,st.sname,COUNT(sc.cid),ROUND(SUM(sc.score),2) AS ' Total score ' FROM t_student st
LEFT JOIN t_score sc ON sc.sid=st.sid GROUP BY st.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 st.* FROM t_student st WHERE sid IN(
SELECT sid FROM t_score WHERE cid = (
SELECT cid FROM t_course WHERE tid = (
SELECT tid FROM t_teacher WHERE tname = ' Zhang San '
)
)
)
#10) Check the information of students who have not learned all the courses
SELECT st.*FROM t_student st WHERE sid NOT IN(
SELECT sid FROM t_score GROUP BY sid HAVING COUNT(cid)>=(SELECT COUNT(cid) FROM t_course)
)
#11) I didn't learn how to query " Zhang San " The name of the student in any course taught by the teacher
SELECT st.sname FROM t_student st WHERE st.sid NOT IN (
SELECT sc.sid FROM t_score sc
INNER JOIN t_course c ON c.cid=sc.cid
INNER JOIN t_teacher t ON t.tid=c.tid AND t.tname=" Zhang San "
)
#12) Check the student number of two or more failed courses , Name and average score
SELECT st.sid,st.sname,AVG(sc.score) FROM t_student st
LEFT JOIN t_score sc ON sc.sid=st.sid WHERE sc.sid IN(
SELECT sc.sid FROM t_score sc WHERE sc.score<60 OR sc.score IS NULL GROUP BY sc.sid HAVING COUNT(1)>=2
)
GROUP BY st.sid
#13) retrieval " 01 " The course score is less than 60, Student information in descending order of scores
SELECT st.*,sc.score FROM t_student st LEFT JOIN t_score sc ON sc.sid=st.sid WHERE sc.cid='01' AND sc.score<60 ORDER BY sc.score DESC ;
#14) Show the grades of all courses and the average grades of all students from high to low
SELECT st.sid,st.sname,
(IFNULL((sc4.score),0)) " average ",
(IFNULL((sc.score),0)) " Chinese language and literature ",
(IFNULL((sc2.score),0)) " mathematics ",
(IFNULL((sc3.score),0))" English "
FROM t_student st
LEFT JOIN t_score sc ON sc.sid=st.sid AND sc.cid="01"
LEFT JOIN t_score sc2 ON sc2.sid=st.sid AND sc2.cid="02"
LEFT JOIN t_score sc3 ON sc3.sid=st.sid AND sc3.cid="03"
LEFT JOIN t_score sc4 ON sc4.sid=st.sid
GROUP BY st.sid
ORDER BY AVG(sc4.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 c.cid,c.cname,MAX(sc.score) " The highest ",MIN(sc.score) " Lowest score ",AVG(sc.score) " average "
,((SELECT COUNT(sid) FROM t_score WHERE score>=60 AND cid=c.cid )/(SELECT COUNT(sid) FROM t_score WHERE cid=c.cid)) " pass rate "
,((SELECT COUNT(sid) FROM t_score WHERE score>=70 AND score<80 AND cid=c.cid )/(SELECT COUNT(sid) FROM t_score WHERE cid=c.cid)) " Medium rate "
,((SELECT COUNT(sid) FROM t_score WHERE score>=80 AND score<90 AND cid=c.cid )/(SELECT COUNT(sid) FROM t_score WHERE cid=c.cid)) " Excellent rate "
,((SELECT COUNT(sid) FROM t_score WHERE score>=90 AND cid=c.cid )/(SELECT COUNT(sid) FROM t_score WHERE cid=c.cid)) " Excellence rate "
FROM t_course c
LEFT JOIN t_score sc ON sc.cid=c.cid
GROUP BY c.cid
1、
2、
3、
4、
5、
6、
7、
8、
9、
10、
边栏推荐
- Fr exercise topic - simple question
- mysql8.0JSON_ Instructions for using contains
- 2022年国内正规的期货公司平台有哪些啊?方正中期怎么样?安全可靠吗?
- Thymeleaf th:classappend attribute append th:styleappend style append th:data- custom attribute
- Jmeter性能测试:ServerAgent资源监控
- 美国费城发生“安全事故” 2名警察遭枪杀
- Is the securities account given by the head teacher of qiniu school safe? Can I open an account?
- 裁员下的上海
- 729. 我的日程安排表 I :「模拟」&「线段树(动态开点)」&「分块 + 位运算(分桶)」
- maxcompute有没有能查询 表当前存储容量的大小(kb) 的sql?
猜你喜欢

Countermeasures of enterprise supply chain management system in UCA Era

Intelligent supply chain collaboration system solution for daily chemical products industry: digital intelligent SCM supply chain, which is the "acceleration" of enterprise transformation

Change multiple file names with one click

两个BI开发,3000多张报表?如何做的到?

729. 我的日程安排表 I :「模拟」&「线段树(动态开点)」&「分块 + 位运算(分桶)」

Fr exercise topic --- comprehensive question

APR protocol and defense

Interpretation of Apache linkage parameters in computing middleware

Loop invariant

Fr exercise topic - simple question
随机推荐
两个BI开发,3000多张报表?如何做的到?
Thymeleaf 模板的创建与使用
在Pytorch中使用Tensorboard可视化训练过程
Longest common subsequence dynamic programming
【NVMe2.0b 14-9】NVMe SR-IOV
maxcompute有没有能查询 表当前存储容量的大小(kb) 的sql?
Microframe technology won the "cloud tripod Award" at the global Cloud Computing Conference!
【華為機試真題詳解】歡樂的周末
Under the crisis of enterprise development, is digital transformation the future savior of enterprises
Pointer operation - C language
PostgreSQL 13 installation
GPS original coordinates to Baidu map coordinates (pure C code)
be careful! Software supply chain security challenges continue to escalate
Topology可视化绘图引擎
浅谈Dataset和Dataloader在加载数据时如何调用到__getitem__()函数
【华为机试真题详解】字符统计及重排
[C question set] of Ⅷ
Thymeleaf 使用后台自定义工具类处理文本
Two policemen were shot dead in a "safety accident" in Philadelphia, USA
Talking about how dataset and dataloader call when loading data__ getitem__ () function
































3、






