create database two20210610 default charset=utf8;
use two20210610;
CREATE TABLE `studentinfo` (
`StudentID` char(10) DEFAULT NULL,
`StudentName` varchar(20) DEFAULT NULL,
`Gender` varchar(2) DEFAULT NULL,
`Birthday` date DEFAULT NULL,
`ClassID` int(4) DEFAULT NULL,
`BeginYear` year(4) DEFAULT NULL,
`Phone` varchar(11) DEFAULT NULL,
`Province` varchar(20) DEFAULT NULL,
`City` varchar(20) DEFAULT NULL,
`Email` varchar(50) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
create table teacher (
id int,
teachername VARCHAR(10),
hiredate DATE,
gender char
);
create table classinfo (
id int, # 班級(jí)編號(hào)
classname varchar(10),
gradeid int, # 年級(jí)編號(hào)
beginyear varchar(10) # 開始年份
);
create table subject (
id int ,
subjectname varchar(10),
teacherid int
);
create table exam (
id int,
exam int,
subjectid int,
studentid int,
remark varchar(10) # 備注
);
create table grade(
id int ,
gradename varchar(10),
major varchar(20)
);
INSERT INTO `two20210610`.`classinfo`(`id`, `classname`, `gradeid`, `beginyear`) VALUES (2001, '20級(jí)1班', 20, '2020');
INSERT INTO `two20210610`.`classinfo`(`id`, `classname`, `gradeid`, `beginyear`) VALUES (2002, '20級(jí)2班', 20, '2020');
INSERT INTO `two20210610`.`exam`(`id`, `exam`, `subjectid`, `studentid`, `remark`) VALUES (1, 98, 101, 1, '無');
INSERT INTO `two20210610`.`exam`(`id`, `exam`, `subjectid`, `studentid`, `remark`) VALUES (2, 89, 102, 1, NULL);
INSERT INTO `two20210610`.`exam`(`id`, `exam`, `subjectid`, `studentid`, `remark`) VALUES (3, 79, 103, 1, '');
INSERT INTO `two20210610`.`exam`(`id`, `exam`, `subjectid`, `studentid`, `remark`) VALUES (4, 96, 104, 1, NULL);
INSERT INTO `two20210610`.`exam`(`id`, `exam`, `subjectid`, `studentid`, `remark`) VALUES (5, 85, 101, 2, NULL);
INSERT INTO `two20210610`.`exam`(`id`, `exam`, `subjectid`, `studentid`, `remark`) VALUES (6, 89, 102, 2, NULL);
INSERT INTO `two20210610`.`exam`(`id`, `exam`, `subjectid`, `studentid`, `remark`) VALUES (7, 79, 103, 2, NULL);
INSERT INTO `two20210610`.`exam`(`id`, `exam`, `subjectid`, `studentid`, `remark`) VALUES (8, 83, 104, 2, NULL);
INSERT INTO `two20210610`.`grade`(`id`, `gradename`, `major`) VALUES (20, '20級(jí)', '軟件技術(shù)');
INSERT INTO `two20210610`.`studentinfo`(`StudentID`, `StudentName`, `Gender`, `Birthday`, `ClassID`, `BeginYear`, `Phone`, `Province`, `City`, `Email`) VALUES ('1', '張無忌', '男', '2021-06-10', 2001, 2020, '13012340001', '河南', '許昌', 'zwj@qq.com');
INSERT INTO `two20210610`.`studentinfo`(`StudentID`, `StudentName`, `Gender`, `Birthday`, `ClassID`, `BeginYear`, `Phone`, `Province`, `City`, `Email`) VALUES ('2', '張鐵牛', '男', '2021-06-16', 2001, 2020, '13012340002', '河南', '許昌', 'ztn@qq.com');
INSERT INTO `two20210610`.`studentinfo`(`StudentID`, `StudentName`, `Gender`, `Birthday`, `ClassID`, `BeginYear`, `Phone`, `Province`, `City`, `Email`) VALUES ('3', '林平之', '男', '2021-06-15', 2001, 2020, '13012340003', '河南', '安陽(yáng)', 'lpz@qq.com');
INSERT INTO `two20210610`.`studentinfo`(`StudentID`, `StudentName`, `Gender`, `Birthday`, `ClassID`, `BeginYear`, `Phone`, `Province`, `City`, `Email`) VALUES ('4', '令狐沖', '男', '2021-06-08', 2002, 2019, '13012340004', '河南', '濮陽(yáng)', 'lhc@qq.com');
INSERT INTO `two20210610`.`studentinfo`(`StudentID`, `StudentName`, `Gender`, `Birthday`, `ClassID`, `BeginYear`, `Phone`, `Province`, `City`, `Email`) VALUES ('5', '岳靈珊', '女', '2021-06-02', 2002, 2019, '13012340005', '河南', '南陽(yáng)', 'yls@qq.com');
INSERT INTO `two20210610`.`subject`(`id`, `subjectname`, `teacherid`) VALUES (101, 'mysql', 201);
INSERT INTO `two20210610`.`subject`(`id`, `subjectname`, `teacherid`) VALUES (102, 'java基礎(chǔ)', 201);
INSERT INTO `two20210610`.`subject`(`id`, `subjectname`, `teacherid`) VALUES (103, '面向?qū)ο?, 202);
INSERT INTO `two20210610`.`subject`(`id`, `subjectname`, `teacherid`) VALUES (104, '網(wǎng)頁(yè)設(shè)計(jì)', 202);
INSERT INTO `two20210610`.`teacher`(`id`, `teachername`, `hiredate`, `gender`) VALUES (201, 'hys', '2021-06-04', '男');
INSERT INTO `two20210610`.`teacher`(`id`, `teachername`, `hiredate`, `gender`) VALUES (202, 'dj', '2021-06-01', '女');
select * from studentinfo;
select * from teacher;
select * from classinfo;
select * from subject;
select * from exam;
select * from grade;
-- 子查詢 用子查詢實(shí)現(xiàn)脚粟,查詢出學(xué)生“林平之”的同班同學(xué)
# 1 找到林平之 的班級(jí)
# 2 查同班同學(xué)
select classid from studentinfo where studentname='林平之';
select * from studentinfo where classid=2001;
select * from studentinfo where classid=(select classid from studentinfo where studentname='林平之');
-- 查詢《mysql》考試成績(jī)剛到等于90的學(xué)生名單
-- 1 需要查詢學(xué)生名單 在studentinfo
-- 2 科目是mysql 在subject表中
-- 3 成績(jī)?yōu)?0 在exam表中subjectid studentid
select studentname from studentinfo inner join exam on studentinfo.StudentID=exam.studentid inner join subject on subject.id=exam.subjectid
where subject.subjectname='mysql' and exam.exam=85;
-- 查詢《mysql》考試成績(jī)剛好等于85分的學(xué)生名單
# 根據(jù)學(xué)生id查詢學(xué)生的名單
select studentname from studentinfo where studentid = ?????
# 在成績(jī)中找到成績(jī)?yōu)?5的學(xué)生id
select studentid from exam where exam=85;
# 在課程中找的mysql的課程ID
select id from where subjectname='mysql';
-- 融合
select studentname from studentinfo where studentid =(select studentid from exam where exam=85 and subjectid = (select id from subject where subjectname='mysql'));
-- 更新"令狐沖"的"mysql"成績(jī)?yōu)?5分
-- 找到mysql成績(jī) +5 exam id subjectid studentid
update exam set exam = exam + 5 where subjectid = ?
-- 找到mysql課程的id subject id subjectname
select id from subject where subjectname = "網(wǎng)頁(yè)設(shè)計(jì)"
-- 根據(jù)名字找id studentinfo
select id from teacher where teachername = "dj"
-- 融合
update exam set exam = exam + 5 where subjectid = (select id from subject where subjectname = "網(wǎng)頁(yè)設(shè)計(jì)" and subject.teacherid = (select id from
teacher where teachername='dj'
))
-- 刪除“張無忌”的所有考試成績(jī)
-- 1 在studentinfo找到岳靈珊的學(xué)生id
select studentid from studentinfo where studentname="張無忌"
-- 2 根據(jù)id在exam表中刪除成績(jī)
delete from exam where studentid = ?
-- 合并
delete from exam where studentid = (select studentid from studentinfo where studentname="張無忌");
-- 插入的子查詢 把查詢結(jié)果作為條件
insert into studentinfo(select * from studentinfo where studentid=5)
select studentname from studentinfo inner join exam on studentinfo.StudentID=exam.studentid inner join subject on subject.id=exam.subjectid
where subject.subjectname='mysql' and exam.exam=85;
-- 查詢《mysql》考試成績(jī)剛好等于85分的學(xué)生名單
# 根據(jù)學(xué)生id查詢學(xué)生的名單
select studentname from studentinfo where studentid = ?
# 在成績(jī)中找到成績(jī)?yōu)?5的學(xué)生id
select studentid from exam where exam=85;
# 在課程中找的mysql的課程ID
select id from subject where subjectname='mysql';
-- 融合
select studentname from studentinfo where studentid in(select studentid from exam where exam=85 and subjectid in
(select id from subject where subjectname='mysql'));
-- 查詢成績(jī)表中編號(hào)為2的考試成績(jī)中是否存在不及格的學(xué)生刨裆,如果存在不及格的學(xué)生就將參加科目編號(hào)102考試的學(xué)生編號(hào)和成績(jī)?nèi)坎樵兲巵?-- 是否存在不及格的學(xué)生
select StudentID from where exam<60
-- 參加科目編號(hào)102考試的學(xué)生
where SubjectID=102
-- 學(xué)生編號(hào)和成績(jī)?nèi)坎樵冿@示出來
select StudentID,Exam from exam
-- 融合
select StudentID,Exam from exam where SubjectID =102 and exists (select StudentID from exam where exam<60);
-- 查詢成績(jī)比科目編號(hào)為”1“的這門課程的所有成績(jī)都大的學(xué)生考試信息
-- 1 查詢····學(xué)生考試成績(jī)
select * from exam where where?
-- 2 成績(jī)比科目編號(hào)為"101"的這門課程的所有成績(jī)都大
any (select exam from exam where SubjectID=1)
-- 合并
select * from exam where exam > any (select exam from exam where SubjectID=101);
SQL高級(jí)子查詢
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來唁毒,“玉大人蒜茴,你說我怎么就攤上這事〗鳎” “怎么了粉私?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)近零。 經(jīng)常有香客問我诺核,道長(zhǎng)抄肖,這世上最難降的妖魔是什么? 我笑而不...
- 正文 為了忘掉前任窖杀,我火速辦了婚禮憎瘸,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘陈瘦。我一直安慰自己,他們只是感情好潮售,可當(dāng)我...
- 文/花漫 我一把揭開白布痊项。 她就那樣靜靜地躺著,像睡著了一般酥诽。 火紅的嫁衣襯著肌膚如雪鞍泉。 梳的紋絲不亂的頭發(fā)上,一...
- 文/蒼蘭香墨 我猛地睜開眼睦刃,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了十酣?” 一聲冷哼從身側(cè)響起涩拙,我...
- 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎耸采,沒想到半個(gè)月后兴泥,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡虾宇,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年搓彻,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片嘱朽。...
- 正文 年R本政府宣布森书,位于F島的核電站靶端,受9級(jí)特大地震影響谎势,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜杨名,卻給世界環(huán)境...
- 文/蒙蒙 一脏榆、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧台谍,春花似錦须喂、人聲如沸。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至掷伙,卻和暖如春是己,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背任柜。 一陣腳步聲響...
- 正文 我出身青樓摔认,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親宅粥。 傳聞我的和親對(duì)象是個(gè)殘疾皇子级野,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- 子查詢是一個(gè)嵌套在SELECT,INSERT粹胯,UPDATE蓖柔,或DELETE語(yǔ)句或其他子查詢中的查詢。 創(chuàng)建數(shù)據(jù)庫(kù)风纠,...
- 知識(shí)點(diǎn)一:Order by#排序Order by在mysql中實(shí)現(xiàn)輸出數(shù)據(jù)的排序(默認(rèn)升序ASC况鸣,降序?yàn)镈ESC)...
- MySQL的基礎(chǔ)查詢語(yǔ)句 基本查詢:SELECT * FROM 表; 字段查詢:SELECT 字段1,字段2 FR...
- 函數(shù)在SQL數(shù)據(jù)庫(kù)中使用十分廣泛誊抛,下面分別介紹了一些比較常用的函數(shù)用法列牺,具體請(qǐng)看下表。 比較懶...就不做表格了拗窃,...
- 【插播】:【SQL】(基礎(chǔ)篇):http://www.reibang.com/p/b0d275039f72 關(guān)于...