明輝手游網(wǎng)中心:是一個(gè)免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺(tái)!

MySQL高級(jí)查詢(xún)之理解與使用案例

[摘要]一.單詞部分①constraint約束②foreign外鍵③references參考④subquery子查詢(xún)⑤inner內(nèi)部的⑥join連接二.預(yù)習(xí)部分1.修改表SQL語(yǔ)句的關(guān)鍵字是什么RENAME 修改表名CHANGE修改字段2.哪個(gè)關(guān)鍵字可以按指定行數(shù)返回查詢(xún)結(jié)果集LIMIT3.表連接都可以用...
一.單詞部分

①constraint約束②foreign外鍵③references參考

④subquery子查詢(xún)⑤inner內(nèi)部的⑥join連接

二.預(yù)習(xí)部分

1.修改表SQL語(yǔ)句的關(guān)鍵字是什么

RENAME 修改表名CHANGE修改字段

2.哪個(gè)關(guān)鍵字可以按指定行數(shù)返回查詢(xún)結(jié)果集

LIMIT

3.表連接都可以用子查詢(xún)替換嗎

可以

三.練習(xí)部分

1. 創(chuàng)建數(shù)據(jù)表,并實(shí)現(xiàn)對(duì)表的修改操作

#上機(jī)1
USE test;
CREATE TABLE person(
number INT(4) AUTO_INCREMENT PRIMARY KEY,
`name` VARCHAR(50) NOT NULL,
sex CHAR(2),
bornDate DATETIME
);
ALTER TABLE person RENAME tb_person;
ALTER TABLE tb_person DROP `bornDate`;
ALTER TABLE tb_person ADD bornDate DATETIME;
ALTER TABLE tb_person CHANGE number id BIGINT;

2.使用SQL語(yǔ)句為myschool數(shù)據(jù)庫(kù)中的result表添加約束

#上機(jī)2
USE myschool;
ALTER TABLE result ADD CONSTRAINT re PRIMARY KEY result(`studentNo`,`subjectNo`,`exameDate`);
ALTER TABLE result ADD CONSTRAINT fk_result_student FOREIGN KEY (studentNo) REFERENCES student (studentNo);

3.為學(xué)生表,科目表,成績(jī)表添加數(shù)據(jù)

#上機(jī)3
USE myschool;
INSERT INTO `subject` (`subjectName`, `classHour`, `gradeID`) VALUES
('HTML', '160', '1'),
('Java OOP', '230', '2');

4.修改學(xué)生表,科目表數(shù)據(jù)

#上機(jī)4
USE myschool;
INSERT INTO `myschool`.`result` (`studentNo`, `subjectNo`, `exameDate`, `studentResult`) VALUES ('1002', '1', '2014-8-8', '78'),
('1003', '2', '2017-7-7', '98'),
('1004', '1', '2015-8-8', '78'),
('1005', '2', '2013-8-8', '78'),
('1006', '1', '2017-8-8', '66'),
('1007', '1', '2014-8-8', '55'),
('1008', '1', '2014-8-8', '13'),
('1009', '1', '2014-8-8', '89');
UPDATE student SET eamil='stu200000@163.com',loginPwd='000' WHERE studentNo='20000';
UPDATE `subject` SET `classHour`=`classHour`-10 WHERE `classHour`>200 AND `subjectNo`=1;
DROP TABLE IF EXISTS student_grade1;
CREATE TABLE student_grade1(SELECT `studentName`,`sex`,`bornDate`,`phone` FROM student WHERE `gradeID`=1);

5.查詢(xún)學(xué)生信息(查詢(xún)2016年2月17日前五名 的學(xué)生的學(xué)號(hào)和分?jǐn)?shù))

#上機(jī)5
SELECT `studentNo`,`studentResult` FROM `result` WHERE `exameDate`<'2016-2-17' ORDER BY studentResult DESC LIMIT 5;

SELECT studentName,(YEAR(NOW())-YEAR(bornDate)) AS age,bornDate,phone FROM student
WHERE sex='女'
ORDER BY bornDate ASC
LIMIT 1,6;

SELECT YEAR(bornDate) AS nian,COUNT(studentNo) AS num FROM student GROUP BY bornDate HAVING COUNT(studentNo)>=2;

SELECT MAX(`studentResult`),MIN(`studentResult`),AVG(`studentResult`) FROM `result` WHERE `exameDate`='2016-02-17' GROUP BY `studentNo`;

6.查詢(xún)指定學(xué)生的考試成績(jī)

#上機(jī)6
SELECT MAX(`studentResult`),MIN(`studentResult`) FROM result
WHERE `exameDate`=(SELECT `exameDate` FROM `result` ORDER BY exameDate DESC LIMIT 1) AND
`subjectNo`=(SELECT `subjectNo` FROM `subject` WHERE `subjectName`='Logic Java');
#select max(exameDate) from result

7.查詢(xún)某學(xué)期開(kāi)設(shè)的課程

#上機(jī)7
SELECT subjectName FROM `subject`
WHERE subjectNo IN(SELECT subjectNo FROM `subject`
WHERE gradeId=(SELECT gradeId FROM grade WHERE gradeName='S1'));

8.查詢(xún)某課程最近一次考試缺考的學(xué)生名單

#上機(jī)8
SELECT `studentName` FROM student WHERE `studentNo` IN(SELECT `studentNo` FROM student WHERE studentNo NOT IN(SELECT `studentNo` FROM `result`
WHERE `subjectNo`=(SELECT `subjectNo` FROM `subject` WHERE `subjectName`='HTML') AND
`exameDate`=(SELECT `exameDate` FROM `result` WHERE subjectNo=(SELECT `subjectNo` FROM `subject` WHERE `subjectName`='HTML')
ORDER BY exameDate DESC LIMIT 1)));

五.總結(jié)部分

多表聯(lián)查實(shí)現(xiàn)的兩種方式:

①表連接

②子查詢(xún)

以上就是MySQL高級(jí)查詢(xún)之理解與使用實(shí)例的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注php中文網(wǎng)其它相關(guān)文章!


學(xué)習(xí)教程快速掌握從入門(mén)到精通的SQL知識(shí)。