MySQL 数据查询

2019-01-02 14:16:53来源:cnblogs.com作者:`Ethan`人点击

分享
  • SELECT子句:用来指定查询返回字段,星号(*)表示返回所有字段

 SELECT  [DISTINCT]*|字段列表        #DISTINCT 用来过滤重复数据 

  • FROM子句:用来指定数据来源的表

FROM <表名> 

  • WHERE子句:用来定义查询返回数据的条件

WHERE 查询条件

  • GROUP BY子句:用来指定查询结果的分组条件

GROUP BY 分组字段 [HAVING 分组条件]

  • ORDER BY子句:用来给指定字段排序,ASC(升序)可省略,DESC(降序)

ORDER BY 排序字段 [ASC|DESC]         #ASC 为升序|DESC为降序

  • LIMIT子句:用来限制select语句返回的记录数

LIMIT [初始位置],记录数

举例说明:

以student、course、score三张表示范, 三张表的创建语句如下

---- Database: stuInfo--CREATE DATABASE stuInfoDEFAULT CHARACTER SET utf8;USE stuInfo;---- 表的结构 student        /*学生表*/--CREATE TABLE student (sNo CHAR(10) NOT NULL COMMENT '学号',sName VARCHAR(20) NOT NULL COMMENT '姓名',sex CHAR(2) NOT NULL DEFAULT '男' COMMENT '性别',birthday DATE NOT NULL COMMENT '出生日期',deptName VARCHAR(30) NOT NULL COMMENT '所属班级',remark VARCHAR(80) COMMENT '备注',PRIMARY KEY (sNo),     /*设置sNo为主键*/INDEX (sName)         /*设置sName为普通索引*/) ENGINE=InnoDB DEFAULT CHARSET=utf8;---- 转存表中的数据 student--INSERT INTO student (sNo, sName, sex, birthday, deptName, remark) VALUES('1308013101', '陈斌', '男', '1993-03-20', '软件131', ''),('1308013102', '张洁', '女', '1996-02-08', '软件131', ''),('1308013103', '郑先超', '男', '1994-04-25', '软件131', ''),('1308013104', '徐孝兵', '男', '1994-08-06', '软件131', ''),('1308013105', '王群', '女', '1995-03-27', '软件131', ''),('1309122501', '刘威', '男', '1994-07-13', '网络131', ''),('1309122502', '沈雁斌', '男', '1994-05-28', '网络131', ''),('1309122503', '杨群', '女', '1995-10-18', '网络131', ''),('1309122504', '蒋维维', '男', '1994-10-19', '网络131', ''),('1309122505', '杨璐', '女', '1995-09-26', '网络131', ''),('1312054901', '王林林', '男', '1994-04-16', '机电131', ''),('1312054902', '杨一超', '男', '1994-08-27', '机电131', ''),('1312054903', '张伟', '男', '1995-01-03', '机电131', ''),('1312054904', '田翠萍', '女', '1994-10-20', '机电131', ''),('1312054905', '周伟', '男', '1995-09-10', '机电131', '');---- 表的结构 course        /*课程表*/--CREATE TABLE course (cNo CHAR(5) NOT NULL COMMENT '课程编号',cName VARCHAR(30) NOT NULL COMMENT '课程名称',credit TINYINT UNSIGNED COMMENT '学分',remark VARCHAR(100) COMMENT '备注',PRIMARY KEY (cNo),     /*设置cNo为主键*/UNIQUE (cName)         /*设置cName为唯一索引*/) ENGINE=InnoDB DEFAULT CHARSET=utf8;---- 转存表中的数据 course--INSERT INTO course (cNo, cName, credit, remark) VALUES('01001', 'C语言程序设计', 5, '计算机类专业课程'),('01002', '数据结构', 4, '计算机类专业课程'),('01003', 'Java程序设计', 4, '计算机类专业课程'),('02001', '网络基础', 3, '计算机类专业课程'),('02002', '数据库原理及应用', 4, '计算机类专业课程'),('02003', '操作系统', 4, '计算机类专业课程'),('09001', '机械设计基础', 5, NULL),('09002', '机械制造基础', 4, NULL),('09003', '机械制图', 4, NULL);---- 表的结构 score        /*成绩表*/--CREATE TABLE score (sNo CHAR(10) NOT NULL COMMENT '学号',cNo CHAR(5) NOT NULL COMMENT '课程编号',grade TINYINT UNSIGNED NOT NULL COMMENT '成绩',PRIMARY KEY(sNo, cNo),     /*设置sNo和cNo为复合主键*/FOREIGN KEY(sNo) REFERENCES student(sNo)    /*与学生表关联*/ON UPDATE NO ACTION ON DELETE NO ACTION,FOREIGN KEY(cNo) REFERENCES course(cNo)    /*与课程表关联*/ON UPDATE NO ACTION ON DELETE NO ACTION) ENGINE=InnoDB DEFAULT CHARSET=utf8;---- 转存表中的数据 score--INSERT INTO score (sNo, cNo, grade) VALUES('1308013101', '01001', 72),('1308013101', '01002', 56),('1308013101', '01003', 77),('1308013102', '01001', 85),('1308013102', '01002', 73),('1308013102', '01003', 90),('1308013103', '01001', 79),('1308013104', '01001', 82),('1308013105', '01001', 63),('1309122501', '02001', 84),('1309122501', '02002', 92),('1309122501', '02003', 71),('1312054901', '09001', 87),('1312054901', '09002', 90),('1312054901', '09003', 95);

 查询语句如下:

 -- 查询学生表的所有字段SELECT * from student; -- 查询学生表的学号,姓名,性别字段SELECT sNo,sName,sex from student;  -- 查询查询学生表的学号,姓名,性别字段,并用 as 定义字段别名SELECT sNo as '学号',sName as '姓名',sex as '性别' FROM student;  -- 查询学生表的所有女生信息SELECT *from student WHERE sex='女';-- 查询课程表中学分大于4的课程所有信息SELECT *FROM course WHERE credit>4;-- 查询学生表中在1995年出生的学生信息select *FROM student where birthday>='1995-1-1' AND birthday<='1995-12-31';-- 查询学生表中姓杨的信息SELECT *FROM student where sName LIKE '杨%';-- 查询学生表中姓杨的姓名为两个字的学生信息SELECT *FROM student where sName LIKE '杨_';-- 查询学生表中姓杨的姓名为三个字的学生信息SELECT *FROM student where sName LIKE '杨__';-- 查询学生表中姓名的第二个字为伟和先的同学信息select *FROM student where sName LIKE '_伟%' OR sName LIKE '_先%';-- 查询学生表中在1995年出生的学生信息SELECT *FROM student where birthday BETWEEN '1995-1-1' AND '1995-12-31';-- 查询成绩表中成绩在60到89之间的信息SELECT *FROM score WHERE grade NOT BETWEEN 60 and 89;-- 查询学生表学号为 1308013101,1309122503,1312054904 的学生信息SELECT *FROM student WHERE sNo in('1308013101','1309122503','1312054904');-- 查询课程表中标记为空的信息SELECT *FROM course where remark is NULL;-- 查询学生表中男生的信息并按生日降序排序SELECT *FROM student WHERE sex='男' ORDER BY birthday DESC;-- 查询学生表所有信息并按性别降序,学号升序排列SELECT *FROM student ORDER BY sex DESC,sNo;-- 查询学生表信息并按生日降序,取前五个记录SELECT *FROM student ORDER BY birthday DESCLIMIT 5;-- 查询成绩表中当课程号为01001的学号,课程号,成绩信息,并以成绩降序排列SELECT sNo,cNo,grade FROM score WHERE cNo='01001' ORDER BY grade DESC LIMIT 1,3;-- 查询成绩表中选修的学号 并用DISTINCT去重SELECT DISTINCT sNo FROM score;-- 用内连接 INNER JOIN 来查询信息SELECT student.sNo,sName,sex,cNo,grade FROM student inner JOIN score ON student.sNo=score.sNowhere sex='女';SELECT student.sNo,sName,sex,cNo,grade FROM student,score where student.sNo=score.sNo and sex='女';-- 查询学号为1308013101 的信息select S.sNo,sName,sex,C.cNo,cName,grade FROM student S INNER JOIN score G ON S.sNo=G.sNoINNER JOIN course C on C.cNo=G.cNowhere S.sNo='1308013101';SELECT S.sNo,sName,sex,C.cNo,cName,grade from student S ,course C,score Gwhere S.sNo=G.sNo and G.cNo=C.cNo and S.sNo='1308013101';-- 查询学号为1308013101的最高分,最低分及平均分和总分信息SELECT max(grade) as '最高分',min(grade) as '最低分' ,avg(grade) as '平均分',sum(grade) as '总分' FROM score WHERE sNo='1308013101' ;-- 查询学生表男生人数SELECT count(*) as '男生人数' from student where sex='男';-- 查询成绩表中已选修课程的人数并用DISTINCT去重select count(DISTINCT sNo)as '已选修课程学生人数' from score;-- 分组统计男、女生人数SELECT sex as '性别',count(*) as '学生人数' from student GROUP BY sex;-- 分组统计不同班级人数SELECT deptName AS '班级',count(*) as '学生人数' from student GROUP BY deptName;-- 5分组查询学生的学号、姓名、选修课程数及平均分SELECT student.sNo as '学号',sName as '姓名', count(*)as '选修课程数',avg(grade) as '平均分' from score INNER JOIN student on score.sNo=student.sNoGROUP BY student.sNo;-- 用嵌套查询学号为1308013101的同学在同一个班级的学生信息SELECT *FROM student where deptName=(SELECT deptName from student where sNo='1308013101');-- 查询课程编号为01001的课程,且成绩超过该课程平均分的学生的学号、姓名、所属班级、课程编号、课程名称及成绩select student.sNo,sName,deptName,course.cNo,cName,grade FROM student,score,course where student.sNo=score.sNo and score.cNo=course.cNo AND course.cNo='01001'and grade>(SELECT avg(grade)FROM score,course where course.cNo=score.cNo and course.cNo='01001');-- 创建新数据表,查询网络131的学生记录,将查询结果插入到新数据表中CREATE TABLE tempStudent(    stuNo char(10) not null PRIMARY KEY,    StuName VARCHAR(20)not NULL,    sex char(1),    deptName varchar(30) not null);INSERT into tempStudent(stuNo,stuName,sex,deptName)  SELECT sNo,sName,sex,deptName from student where deptName='网络131';-- 将数据结构 课程成绩统一减去5update score SET grade=grade-5where cNo=(SELECT cNO from course WHERE cName='数据结构');-- 查询更新完的成绩表select *from score where cNo=(SELECT cNO from course WHERE cName='数据结构');-- 删除机电131的学生的成绩记录delete from scorewhere sNo in (SELECT sNo from student where deptName='机电131');-- 查询 删除后的机电131成绩记录SELECT *from score where sNo in (SELECT sNo from student where deptName='机电131');

相关文章

    无相关信息

最新文章

123

最新摄影

闪念基因

微信扫一扫

第七城市微信公众平台