素材如下:
DROP TABLE IF EXISTS `course`;
CREATE TABLE `course` (
? `cs_id` int(11) NOT NULL COMMENT '課程編號',
? `cs_name` varchar(50) NOT NULL COMMENT '課程名稱',
? `cs_credit` tinyint(255) unsigned DEFAULT NULL COMMENT '課程學分',
? `cs_type` char(12) DEFAULT NULL COMMENT '課程類別',
? `cs_depart` char(6) DEFAULT NULL COMMENT '院系名稱',
? PRIMARY KEY (`cs_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*Data for the table `course` */
insert? into `course`(`cs_id`,`cs_name`,`cs_credit`,`cs_type`,`cs_depart`) values (5200313,'數據庫原理及應用',4,'核心專業','信工'),(5203314,'計算機導論',4,'通識教育','信工'),(5219314,'數據結構',5,'專業核心','信工'),(5223013,'大學物理',4,'專業基礎','信工'),(5227614,'畢業實習',4,'集中實踐','信工'),(5230912,'云計算',2,'共同選修','信工'),(5236212,'機器學習',2,'共同選修','信工'),(5237514,'c語言',4,'專業基礎','信工'),(5245112,'區塊鏈',2,'任意選修','信工'),(7200422,'知識產權法',2,'任意選修','文法'),(20201833,'概率論',3,'專業基礎','基礎'),(20202336,'高等數學',6,'專業基礎','基礎'),(29299131,'勞動教育',1,'集中實踐','學務');
/*Table structure for table `student` */
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
? `stu_id` bigint(11) unsigned NOT NULL COMMENT '學號',
? `stu_name` char(12) NOT NULL COMMENT '姓名',
? `stu_sex` enum('男','女') DEFAULT NULL COMMENT '性別',
? `stu_age` tinyint(255) unsigned DEFAULT NULL COMMENT '年齡',
? `stu_major` char(9) DEFAULT NULL COMMENT '專業',
? `stu_college` char(12) DEFAULT NULL COMMENT '學院',
? PRIMARY KEY (`stu_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*Data for the table `student` */
insert? into `student`(`stu_id`,`stu_name`,`stu_sex`,`stu_age`,`stu_major`,`stu_college`) values (201804550101,'郭奎','男',22,'計科','信工學院'),(201804550102,'呂宇航','男',18,'計科','信工學院'),(201804550103,'張豪輝','女',19,'計科','信工學院'),(201804550107,'丁志杰','男',17,'金融學','金貿學院'),(201804550109,'范偉','男',19,'金融學','金貿學院'),(201804550116,'張依婷','女',17,'大數據','信工學院'),(201804550120,'張維','男',19,'計科','信工學院'),(201804550121,'朱柳陽','女',20,'計科','信工學院'),(201804550144,'譚兵炎','男',20,'大數據','信工學院'),(201804550153,'楊志強','男',17,'大數據','信工學院');
下面的例題我們以這兩個表作為實驗:
?
1.查詢全部課程的信息。?
select cs_name from course;
2.查詢信工學院開設的課程名、課程號及學分。?
select? cs_id,cs_name,cs_credit from course where cs_depart = '信工';
3.查詢學分超過3學分的課程代碼、課程名和開課單位。
select cs_id,cs_name,cs_depart from course where cs_credit > '3' ;
4.查詢計科專業和大數據專業的學生信息。
select * from student where stu_major = '計科' or stu_major = '大數據' ;
5.查詢不是信工學院的學生姓名和學號。
select * from student where stu_college != '信工學院';
6.查詢年齡是17,18,19的學生姓名和專業。?
select * from student where stu_age = '17' or stu_age = '18' or stu_age = '19';
7.查詢學分在2到4之間課程的信息。?
select * from course where cs_credit BETWEEN 2 AND 4;
8.查詢課程名稱中帶“數據”的課程名、課程號及開課單位。
select cs_id,cs_name,cs_depart from course where? cs_name like '%數據%' ;
9.查詢信工學院的的專業有哪些。?
select cs_name from course where cs_depart = '信工';
10.查詢年齡為空的學生信息。?
select * from student where stu_age is NULL;
11.查詢不是信工學院開設的集中實踐課的開課單位和課程名稱。?
select * from course where cs_type = '集中實踐' and cs_depart != '信工';
12.查詢信工學院開設的課程的類型有哪些。?
select distinct cs_type from course where cs_depart = '信工';
distinct 去重查詢
13.查詢學生所在的專業個數。?
select distinct stu_major from student ;
14.查詢信工學院開設的課程的平均學分。?
select avg(cs_credit) from course where cs_depart = '信工';
15.查詢學生的信息,查詢結果按姓名升序排序。?
select * from student order by stu_name asc ;
desc是descend 降序意思
asc 是ascend 升序意思
16.查詢 每個專業的學生的最大年齡、最小年齡和平均年齡,查詢結果按平均年齡降序排列。
select max(stu_age),min(stu_age),avg(stu_age) from student group by stu_major order by avg(stu_age) desc ;
17.查詢每個開課單位開設的課程門數的,查詢結果按課程門數升序排列。?
select cs_depart,count(1) from course group by cs_depart order by count(1);
18.查詢單位開課門數少于2門的開課單位和課程名稱。
select cs_depart,cs_name from course group by cs_depart having count(*) < 2;
?