当前位置:网站首页>Mysql database (III) advanced data query statement

Mysql database (III) advanced data query statement

2022-07-06 15:10:00 Hand pluckable Xinchen

1, Multiple tables associated query

Non equivalent query

grammar :select * from surface 1, surface 2

#  Multiple tables associated query 
--  Non equivalent associated query 
--  The cartesian product 
SELECT * FROM student,class;

Equivalent query

(1) Inline query

grammar :select * from surface 1, surface 2 where surface 1. Field 1 = surface 2. Field 2...

--  Equivalent joint query 
--  Inline query 
SELECT * FROM student,class,sc WHERE student.classid = class.classid AND student.sid  = sc.sid;

SELECT * FROM sc,course WHERE sc.cid = course.cid;

SELECT DISTINCT * FROM student,class,sc,teacher,course WHERE student.Sid =  sc.Sid AND sc.Cid = course.Cid AND course.Tid = teacher.Tid AND student.classid = class.classid;

--  Find the students taught by teacher Zhang San 
SELECT student.Sid,Sname,birthday,Ssex,classid FROM teacher,student,sc,course 
WHERE teacher.Tid = course.Tid 
AND course.Cid = sc.Cid 
AND sc.Sid = student.Sid 
AND Tname = ' Zhang San ';
--  Mr. Zhang San taught the number of students , Average test score 
SELECT COUNT(*)  Number of students  ,AVG(score)  average  FROM teacher,student,sc,course 
WHERE teacher.Tid = course.Tid 
AND course.Cid = sc.Cid 
AND sc.Sid = student.Sid 
AND Tname = ' Zhang San ';
--  The number and average score of students taught by each teacher 
SELECT COUNT(*)  Number of students  ,AVG(score)  average ,teacher.Tname FROM teacher,student,sc,course 
WHERE teacher.Tid = course.Tid 
AND course.Cid = sc.Cid 
AND sc.Sid = student.Sid GROUP BY teacher.Tid ORDER BY AVG(score) DESC;

(3) The second way to write an inline query inner join on 

grammar :select * from surface 1 inner join surface 2 on Conditions

--  The second way to write inline 
--  surface  INNER JOIN ON  Relationship 
SELECT * FROM student 
INNER JOIN class ON student.classid = class.classid 
INNER JOIN sc ON student.Sid = sc.Sid
INNER JOIN course ON sc.Cid = course.Cid
INNER JOIN teacher ON teacher.Tid =  course.Tid
WHERE Tname = ' Zhang San ';

(4) Outreach query left join in or right join on

grammar :select * from surface 1 left or right  join surface 2 on Conditions ;

--  You need to select a main table for the external query 
--  Left outreach 
--  surface  LEFT JOIN  surface  ON  Relationship 
SELECT * FROM student LEFT JOIN class ON student.classid = class.classid; 
--  Right outreach 
--  surface  RIGHT JOIN  surface  ON  Relationship 
SELECT * FROM class RIGHT JOIN student ON student.classid = class.classid;

SELECT Sname,classname,Cname,score,Tname FROM student 
LEFT JOIN class ON student.classid = class.classid 
RIGHT JOIN sc ON student.Sid = sc.Sid 
LEFT JOIN course ON course.Cid =sc.Cid
INNER JOIN teacher ON teacher.Tid =course.Tid;
--  Find students who have no classes 
SELECT * FROM student LEFT JOIN class ON student.classid = class.classid 
WHERE class.classid IS NULL;
--  Find a class without students 
SELECT * FROM class LEFT JOIN student ON student.classid = class.classid 
WHERE student.sid IS NULL; 
--  Students who have not passed the exam 
SELECT * FROM student LEFT JOIN sc on student.Sid = sc.Sid 
WHERE sc.score is NULL;

(5)union Find the union of two queries  

grammar :select A.field1 as f1, A.field2 as f2 from <table1> A union select B.field3 as f3, A.field4 as f4 from <table2> B;

Be careful :

        1, When the column names are inconsistent , The header of the first table will prevail , And its columns .

        2, Duplicate rows will be filtered out .

        3, If the number of columns in the queried table is not equal , Will report a mistake .

        4, Sorting in each clause is meaningless ,mysql It will be ignored when merging .

        5, If the sort and in the clause limit It makes sense to combine .        

        6, You can sort the consolidated whole table .

        7,union It filters duplicate data ,union all Duplicate data is not filtered .

-- UNION  Find the union of two sets 
--  Data type doesn't matter 
--  Two results 

--  Find classes without students and students without classes 
SELECT * FROM student LEFT JOIN class ON student.classid = class.classid 
WHERE class.classid IS NULL
UNION
SELECT * FROM class LEFT JOIN student ON student.classid = class.classid 
WHERE student.sid IS NULL;

SELECT * FROM student RIGHT JOIN class ON student.classid = class.classid
UNION
SELECT * FROM class RIGHT JOIN student ON student.classid = class.classid;

--  Get information about all classes and students 
-- union  Remove duplicate data   
--  add to  all  No longer remove duplicates , All show 
select * from student left join class
	on student.classid = class.classid
union all
select * from student right join 	class
	on student.classid = class.classid

2, Subquery Internal query

(1)where type

#  Subquery 
--  All subqueries must be enclosed in parentheses 
-- where  Subquery 
--  Inquire about  id  The biggest student ( Sort , Pagination )
SELECT * FROM student ORDER BY Sid DESC LIMIT 1;
-- max  maximal id
SELECT * FROM student WHERE Sid = (SELECT MAX(Sid) FROM student);
--  Students who have studied teacher Zhang San's course 
SELECT * FROM student WHERE Sid IN (SELECT Sid FROM sc WHERE cid = (SELECT cid FROM course WHERE Tid = (SELECT Tid FROM teacher WHERE tname = ' Zhang San ' )));
--  Students who have not studied teacher Zhang San's course 
SELECT * FROM student WHERE Sid NOT IN (SELECT Sid FROM sc WHERE cid = (SELECT cid FROM course WHERE Tid = (SELECT Tid FROM teacher WHERE tname = ' Zhang San ' )));
--  Inquire about , Every class id The biggest student ( Use where Sub query implementation )
SELECT * FROM student,class WHERE student.classid = class.classid AND Sid IN (SELECT MAX(Sid) FROM student GROUP BY classid);
SELECT * FROM student LEFT JOIN class ON student.classid = class.classid WHERE  Sid IN (SELECT MAX(Sid) FROM student GROUP BY classid);
--  Query is greater than or equal to 2 Class name and number of people 
SELECT classname,COUNT(Sid) FROM class LEFT JOIN student ON student.classid = class.classid GROUP BY class.classid HAVING COUNT(sid) >= 2;

(2)from type   Take the query results of the inner layer as a temporary table , For the outer layer sql Query again .

-- FROM  Subquery 
--  Query is greater than or equal to 5 Class name and number of people 
SELECT classname,COUNT(Sid) FROM class LEFT JOIN student ON student.classid = class.classid GROUP BY class.classid HAVING COUNT(sid) >= 2;
-- FROM  Subquery    Query is greater than or equal to 5 Class name and number of people 
-- FROM  Subquery   A table name should be given to the subquery 
SELECT classname, The number of  FROM class INNER JOIN 
(SELECT classid,COUNT(*)  The number of  FROM student GROUP BY classid ) t1 ON class.classid = t1.classid 
WHERE  The number of  >= 4;
-- FROM  Subquery  
--  The number of exams and average score of each class 
--  Class name   Number of examinations , average 
SELECT * FROM class LEFT JOIN (SELECT classid,COUNT(sc.sid),AVG(score) FROM student LEFT JOIN sc ON student.Sid = sc.Sid GROUP BY classid) t1 ON class.classid = t1.classid;

SELECT classname  class ,COUNT(sc.Sid)  Number of examinations ,AVG(score)  average  FROM student,sc,class WHERE student.classid = class.classid AND student.Sid = sc.Sid GROUP BY class.classid;

(3)exists type   Put the outer layer sql Result , Get the inner layer sql To test , If the inner layer sql establish , Then the line takes out . The inner query is exists After the query .

-- EXISTS  Subquery 
SELECT * FROM teacher WHERE EXISTS (SELECT * FROM student WHERE Ssex = ' male ');

(4)any,some,all 

-- ANY SOME ALL  Subquery 
--  Find out the students whose grades in class 1 are higher than the lowest grades in class 2 
SELECT DISTINCT student.* FROM student INNER JOIN sc ON student.Sid = sc.Sid 
WHERE classid = 1 AND score > ANY (SELECT score FROM student INNER JOIN sc ON student.Sid = sc.Sid WHERE classid = 2);
SELECT DISTINCT student.* FROM student INNER JOIN sc ON student.Sid = sc.Sid 
WHERE classid = 1 AND score > SOME (SELECT score FROM student INNER JOIN sc ON student.Sid = sc.Sid WHERE classid = 2);
-- ALL  Subquery 
--  Inquire about 
SELECT DISTINCT student.* FROM student INNER JOIN sc ON student.Sid = sc.Sid 
WHERE classid = 1 AND score > ALL (SELECT score FROM student INNER JOIN sc ON student.Sid = sc.Sid
WHERE classid = 2);

3, Process control functions

(1)if(expr1,expr2,expr3)      If expr1 Conditions established , execute expr2, If the conditions don't hold , execute expr3

-- IF(expr1,expr2,expr3)
-- expr1  Conditions 
-- expr2  The result of the condition 
-- expr3  The result that the condition does not hold 
SELECT tid,tname,IF(tsex = '1',' male ',' Woman ')  Gender ,tbirthday
FROM teacher;

(2)ifnull(expr1,expr2)         If expr1 Not for Null, execute expr1, If expr1 by Null, execute expr2

-- IFNULL (expr1,expr2)
-- expr1  Field name 
-- expr2  The default value is 
SELECT sid,sname,IFNULL(birthday,'2000-2-2') FROM student;

(3)case when then end

① Simple case function

grammar :case Field name when Constant then result end;

--  Simple case
-- CASE  Field name  WHEN  Constant  THEN  result  END;
SELECT Tid,tname,CASE tsex WHEN 1 THEN ' male ' WHEN 0 THEN ' Woman ' ELSE ' Unknown ' END,temail,tmoney FROM teacher;

② Search element case function

grammar :case when Conditions then result end;

--  Search element case  Commonly used 
-- CASE WHEN  Conditions  THEN  result  END
SELECT tid,tname,CASE WHEN tsex = 1 THEN ' male ' WHEN Tsex <> 1 THEN ' Woman ' END,temail,tmoney FROM teacher; 
--  Check the student's grades , And will be greater than 90 Use of points A Show , Greater than 80 Use of points B Show , Greater than 70 Use of points C Show , Greater than 60 Use of points D Show , Less than 60 The score shows that I failed 
SELECT student.Sid,Sname,birthday,Ssex,classid, CASE 
WHEN score >= 90 AND score <= 100 THEN 'A'
WHEN score >= 80 AND score < 90 THEN 'B'
WHEN score >= 70 AND score < 80 THEN 'C'
WHEN score >= 60 AND score < 70 THEN 'D'
WHEN score < 60 THEN ' fail, ' END  achievement 
FROM student INNER JOIN sc ON student.Sid = sc.Sid ;

原网站

版权声明
本文为[Hand pluckable Xinchen]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060919160232.html