Example: MySQL uses DQL commands to query data

DROP TABLE IF EXISTS `grade`;

CREATE TABLE `grade` (
  `GradeID` INT(11) NOT NULL AUTO_INCREMENT COMMENT 'Grade ID',
  `GradeName` VARCHAR(50) NOT NULL COMMENT 'GradeName',
  PRIMARY KEY (`GradeID`)
) ENGINE=INNODB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
 
INSERT INTO `grade`(`GradeID`,`GradeName`) VALUES (1,'Freshman'),(2,'Sophomore'),(3,'Junior'),(4,'Senior') ,(5,'preparatory class');
  
DROP TABLE IF EXISTS `result`;

CREATE TABLE `result` (
  `StudentNo` INT(4) NOT NULL COMMENT '学号',
  `SubjectNo` INT(4) NOT NULL COMMENT 'SubjectNo',
  `ExamDate` DATETIME NOT NULL COMMENT 'Exam Date',
  `StudentResult` INT(4) NOT NULL COMMENT 'Exam Result',
  KEY `SubjectNo` (`SubjectNo`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
 

INSERT INTO `result`(`StudentNo`,`SubjectNo`,`ExamDate`,`StudentResult`) VALUES (1000,'111111','Guo Jing',1,1,'13500000001','No.1 Zhongguancun Street, Haidian District, Beijing ','1986-12-11 00:00:00','[email protected]','450323198612111234'),(1001,'123456','Li Wencai',1,2,'13500000002','Henan Luoyang','1981-12-31 00:00:00','[email protected]','450323198112311234'),(1002,'111111','Li Siwen',1,1,'13500000003',' Heping District, Tianjin','1986-11-30 00:00:00','[email protected]','450323198611301234'),(1003,'123456','Wusong',1,3,'13500000004' ,'Shanghai Luwan District','1986-12-31 00:00:00','[email protected]','450323198612314234'),(1004,'123456','Zhang San',1,4,' 13500000005','Beijing Tongzhou','1989-12-31 00:00:00','[email protected]','450323198612311244'),(1005,'123456','Zhang Qiuli',2,1,'13500000006','Lingchuan,Guilin,Guangxi','1986-12-31 00:00:00','test1 @163.cn','450323198612311214'),(1006,'123456','Xiao Mei',2,4,'13500000007','Address unknown','1986-12-31 00:00:00',' [email protected]','450323198612311134'),(1007,'111111','Ouyang Junfeng',1,1,'13500000008','Beijing Dongcheng District','1986-12-31 00:00:00 ','[email protected]','450323198612311133'),(1008,'111111','Mei Chaofeng',1,1,'13500000009','Henan Luoyang','1986-12-31 00:00 :00','[email protected]','450323198612311221'),(1009,'123456','Liu Yi',1,2,'13500000011','Anhui','1986-12-31 00:00 :00','[email protected]','450323198612311231'),(1010,'111111','Dafan',1,1,'13500000012','Henan Luoyang','1986-12-31 00:00:00','[email protected]','450323198612311044'),(1011,' 111111','Odance',1,1,'13500000013','No.* Zhongguancun Street, Haidian District, Beijing','1984-12-31 00:00:00','[email protected]','450323198412311234 '),(1012,'123456','Duolun',2,3,'13500000014','Guangxi Nanning Central Street','1986-12-31 00:00:00','[email protected]' ,'450323198612311334'),(1013,'123456','Li Mei',2,1,'13500000015','Shanghai Luwan District','1986-12-31 00:00:00','test1@163. cn','450323198612311534'),(1014,'123456','Zhang De',2,4,'13500000016','No.* Zhongguancun Street, Haidian District, Beijing','1986-12-31 00:00:00' ,'[email protected]','450323198612311264'),(1015,'123456','Li Dongfang',1,4,'13500000017','Lingchuan,Guilin,Guangxi','1976-12-31 00:00:00','[email protected]','450323197612311234'),(1016,'111111','Liu Fendi',1 ,1,'13500000018','Shanghai Luwan District','1986-12-31 00:00:00','[email protected]','450323198612311251'),(1017,'123456','Cocoa', 2,3,'13500000019','No.1 Chang'an Street, Beijing','1981-09-10 00:00:00','[email protected]','450323198109108311'),(10066,'','Tom ',1,1,'13500000000','','0000-00-00 00:00:00','[email protected]','33123123123123123');450323198612311251'),(1017,'123456','Coco',2,3,'13500000019','No.1 Chang'an Street,Beijing','1981-09-10 00:00:00','[email protected] ','450323198109108311'),(10066,'','Tom',1,1,'13500000000','','0000-00-00 00:00:00','[email protected]',' 33123123123123123');450323198612311251'),(1017,'123456','Coco',2,3,'13500000019','No.1 Chang'an Street,Beijing','1981-09-10 00:00:00','[email protected] ','450323198109108311'),(10066,'','Tom',1,1,'13500000000','','0000-00-00 00:00:00','[email protected]',' 33123123123123123');

DROP TABLE IF EXISTS `student`;

CREATE TABLE `student` (
  `StudentNo` INT(4) NOT NULL COMMENT '学号',
  `LoginPwd` VARCHAR(20) DEFAULT NULL,
  `StudentName` VARCHAR(20) DEFAULT NULL COMMENT 'Student Name',
  `Sex` TINYINT(1) DEFAULT NULL COMMENT 'Sex, value 0 or 1',
  `GradeId` INT(11) DEFAULT NULL COMMENT 'GradeId',
  `Phone` VARCHAR(50) NOT NULL COMMENT 'Contact phone number, it is allowed to be empty, you can choose to input',
  `Address` VARCHAR(255) NOT NULL COMMENT 'Address, it is allowed to be empty, you can select input',
  `BornDate` DATETIME DEFAULT NULL COMMENT 'Born time',
  `Email` VARCHAR(50) NOT NULL COMMENT 'Email account, it is allowed to be empty, you can choose to input',
  `IdentityCard` VARCHAR(18) DEFAULT NULL COMMENT 'Identity Card',
  PRIMARY KEY (`StudentNo`),
  UNIQUE KEY `IdentityCard` (`IdentityCard`),
  KEY `Email` (`Email`)
) ENGINE=MYISAM DEFAULT CHARSET=utf8;
 
INSERT INTO `student`(`StudentNo`,`LoginPwd`,`StudentName`,`Sex`,`GradeId`,`Phone`,`Address`,`BornDate`,`Email`,`IdentityCard`) VALUES (1000, '111111','Guo Jing',1,1,'13500000001','No.1 Zhongguancun Street, Haidian District, Beijing','1986-12-11 00:00:00','[email protected]','450323198612111234' ),(1001,'123456','Li Wencai',1,2,'13500000002','Henan Luoyang','1981-12-31 00:00:00','[email protected]','450323198112311234 '),(1002,'111111','Li Siwen',1,1,'13500000003','Heping District of Tianjin','1986-11-30 00:00:00','[email protected]' ,'450323198611301234'),(1003,'123456','Wusong',1,3,'13500000004','Shanghai Luwan District','1986-12-31 00:00:00','[email protected] ','450323198612314234'),(1004,'123456','Zhang San',1,4,'13500000005','Beijing Tongzhou','1989-12-31 00:00:00','[email protected]','450323198612311244'),(1005,'123456','Zhang Qiuli',2,1,'13500000006' ,'Guangxi Guilin Lingchuan','1986-12-31 00:00:00','[email protected]','450323198612311214'),(1006,'123456','Xiao Mei',2,4 ,'13500000007','Address unknown','1986-12-31 00:00:00','[email protected]','450323198612311134'),(1007,'111111','Ouyang Junfeng',1 ,1,'13500000008','Beijing Dongcheng District','1986-12-31 00:00:00','[email protected]','450323198612311133'),(1008,'111111','Mei Chaofeng ',1,1,'13500000009','Henan Luoyang','1986-12-31 00:00:00','[email protected]','450323198612311221'),(1009,'123456','Liu Yi',1,2,'13500000011','Anhui','1986-12-31 00:00:00','[email protected]','450323198612311231'),(1010,'111111','Dafan',1,1,'13500000012','Henan Luoyang','1986-12-31 00:00:00', '[email protected]','450323198612311044'),(1011,'111111','Odance',1,1,'13500000013','No.* Zhongguancun Street, Haidian District, Beijing','1984-12-31 00:00:00','[email protected]','450323198412311234'),(1012,'123456','Duolun',2,3,'13500000014','Guangxi Nanning Central Street','1986- 12-31 00:00:00','[email protected]','450323198612311334'),(1013,'123456','Li Mei',2,1,'13500000015','Shanghai Luwan District',' 1986-12-31 00:00:00','[email protected]','450323198612311534'),(1014,'123456','Zhang De',2,4,'13500000016','Zhongguancun, Haidian District, Beijing Street * No.','1986-12-31 00:00:00','[email protected]','450323198612311264'),(1015,'123456','Li Dongfang',1,4,'13500000017','Lingchuan,Guilin,Guangxi','1976-12-31 00:00:00','test1@bdqn. cn','450323197612311234'),(1016,'111111','Liu Fend',1,1,'13500000018','Shanghai Luwan District','1986-12-31 00:00:00','test1@ bdqn.cn','450323198612311251'),(1017,'123456','Coco',2,3,'13500000019','Beijing Chang'an Avenue 1','1981-09-10 00:00:00', '[email protected]','450323198109108311'),(10066,'','Tom',1,1,'13500000000','','0000-00-00 00:00:00','email@ 22.com','33123123123123123');'111111','Liu Fendi',1,1,'13500000018','Shanghai Luwan District','1986-12-31 00:00:00','[email protected]','450323198612311251'),( 1017,'123456','Coco',2,3,'13500000019','No.1 Chang'an Street,Beijing','1981-09-10 00:00:00','[email protected]','450323198109108311' ),(10066,'','Tom',1,1,'13500000000','','0000-00-00 00:00:00','[email protected]','33123123123123123');'111111','Liu Fendi',1,1,'13500000018','Shanghai Luwan District','1986-12-31 00:00:00','[email protected]','450323198612311251'),( 1017,'123456','Coco',2,3,'13500000019','No.1 Chang'an Street,Beijing','1981-09-10 00:00:00','[email protected]','450323198109108311' ),(10066,'','Tom',1,1,'13500000000','','0000-00-00 00:00:00','[email protected]','33123123123123123');','0000-00-00 00:00:00','[email protected]','33123123123123123');','0000-00-00 00:00:00','[email protected]','33123123123123123');

DROP TABLE IF EXISTS `subject`;

CREATE TABLE `subject` (
  `SubjectNo` INT(11) NOT NULL AUTO_INCREMENT COMMENT 'course number',
  `SubjectName` VARCHAR(50) DEFAULT NULL COMMENT 'SubjectName',
  `ClassHour` INT(4) DEFAULT NULL COMMENT 'Hours',
  `GradeID` INT(4) DEFAULT NULL COMMENT 'Grade ID',
  PRIMARY KEY (`SubjectNo`)
) ENGINE=INNODB AUTO_INCREMENT=18 DEFAULT CHARSET=utf8;
  
INSERT INTO `subject`(`SubjectNo`,`SubjectName`,`ClassHour`,`GradeID`) VALUES (1,'Advanced Math-1',110,1),(2,'Advanced Math-2',110, 2),(3,'Advanced Mathematics-3',100,3),(4,'Advanced Mathematics-4',130,4),(5,'C Language-1',110,1),(6 ,'C language-2',110,2),(7,'C language-3',100,3),(8,'C language-4',130,4),(9,'JAVA first School year',110,1),(10,'JAVA second school year',110,2),(11,'JAVA third school year',100,3),(12,'JAVA fourth school year',130, 4),(13,'Database structure-1',110,1),(14,'Database structure-2',110,2),(15,'Database structure-3',100,3),(16 ,'Database structure-4',130,4),(17,'C# basic',130,1);
 

 

#select concat('student:',studentname) student name,concat('+86',phone) phone from student;

# select count(studnetno) total number of students from student;

#DISTINCT keyword function: remove duplicate records in the record results returned by the SELECT query (all returned columns have the same value), and only return one Syntax: SELECT DISTINCT field name 1, field name 2... FROM table name
#Note: The ALL keyword is the default and returns all records, as opposed to
Use of #DISTINCTKeywords
#Query the course ID contained in the grade table
SELECT   DISTINCT  SubjectNo  FROM  result;

#BETWEEN AND QUERY
SELECT  *  FROM  SUBJECT  WHERE     ClassHour   BETWEEN  110   AND  120;
#Equivalent to:
SELECT  *  FROM  SUBJECT  WHERE     ClassHour >= 110  AND  ClassHour <=120;

#Query the course name, total class hours and the number of days calculated by 10 class hours per day
SELECT  
SubjectName AS "course name",
ClassHour AS "Total Class Hours",
(ClassHour/10) AS "Average Class Hour/Day"
FROM  SUBJECT;

#Query all courses containing "math"
SELECT   *  FROM SUBJECT WHERE SubjectName  LIKE  "%数学%";
#Query the information of all students whose name is "Li**"
 SELECT  StudentNo,StudentName FROM student  WHERE StudentName
LIKE "李__";

#Case to find student information whose address is not empty
SELECT  *
FROM  student
WHERE  Address IS NOT NUL

# inner join left join right link
SELECT s.studentno,s.studentname,g.Gradename FROM
student s INNER JOIN `grade` g ON g.gradeid=s.GradeID;
SELECT s.studentno,s.studentname,g.Gradename FROM
student s LEFT JOIN `grade` g ON g.gradeid=s.GradeID;
SELECT s.studentno,s.studentname,g.Gradename FROM
student s  RIGHT JOIN `grade` g ON g.gradeid=s.GradeID;

#Use IN for range query
SELECT * FROM SUBJECT WHERE ClassHour = 100 OR ClassHour =110 OR ClassHour = 120; #Ordinary group processing method
SELECT * FROM SUBJECT WHERE ClassHour IN ( 100, 110,120 );#Use IN to query, which is more concise and efficient

#subquery in
SELECT studentno,studentname
FROM student
WHERE StudentNo IN
(SELECT studentno FROM result WHERE
subjectno=(SELECT subjectno FROM `subject` WHERE subjectname='高等数学-2' )
AND StudentResult>=80
 ) ;

#Common statistical functions
#COUNT(),MAX(),MIN(),AVG(),SUM
SELECT COUNT(StudentResult) AS "Total Score", MAX(StudentResult) AS "Highest Score" , MIN(StudentResult) AS "Lowest Score" , AVG(StudentResult) AS "Average Score"
FROM result;

#According to different courses, calculate the average score, the highest score and the lowest score respectively, and the average score below 60 will not be displayed
SELECT
s.SubjectName AS "course name", MAX(StudentResult) AS "highest score" , MIN(StudentResult) AS "lowest score" , AVG(StudentResult) AS "average score"
FROM
result  AS r
LEFT JOIN
`subject` AS s ON s.SubjectNo = r.SubjectNo
GROUP BY  r.SubjectNo HAVING   AVG(StudentResult) >= 60 ;

# ORDER BY sort
SELECT studentresult
 FROM result r WHERE
 r.subjectno IN(SELECT subjectno FROM `subject` WHERE subjectname="数据库结构-1" )
ORDER BY studentresult DESC ; #default ASC

#Query the test scores of all courses "Database Structure-2", and display them from high to low, and print out the student number and name of the student corresponding to the score
#Require:
#First query the first 10 records
#The second query 11-20 records
SELECT s.studentno,s.studentname,c.subjectname,r.studentresult
FROM student s JOIN result r
ON s.StudentNo=r.StudentNo
JOIN SUBJECT c
ON r.SubjectNo=c.SubjectNo
WHERE c.subjectname="database structure-2"
ORDER BY r.StudentResult DESC LIMIT 0,5;  #limit 5,5

#Requirement: Query the course name and the grade name from the subject and grade data tables
#Non-equivalent join query
SELECT SubjectName,GradeName FROM SUBJECT,grade;  
#equivalent query
SELECT SubjectName,GradeName FROM SUBJECT,grade WHERE  subject.GradeID= grade.GradeID;  .GradeID;

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326655600&siteId=291194637