4.DQL查询数据(最重点!!!)
1.DQL的特点
(data query language:数据库查询语言)
- 所有的查询操作都用它 select
- 简单的查询,复杂的查询它都能做
- 数据库中最核心的语言,最重要的语句☆
- 使用频率最高的语句
创库语句:
create database if not exists `school`;
-- 创建一个school数据库
use `school`;-- 创建学生表
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 '学生姓名',
`sex` tinyint(1) default null comment '性别,0或1',
`gradeid` int(11) default null comment '年级编号',
`phone` varchar(50) not null comment '联系电话,允许为空',
`address` varchar(255) not null comment '地址,允许为空',
`borndate` datetime default null comment '出生时间',
`email` varchar (50) not null comment '邮箱账号允许为空',
`identitycard` varchar(18) default null comment '身份证号',
primary key (`studentno`),
unique key `identitycard`(`identitycard`),
key `email` (`email`)
)engine=myisam default charset=utf8;
-- 创建年级表
drop table if exists `grade`;
create table `grade`(
`gradeid` int(11) not null auto_increment comment '年级编号',
`gradename` varchar(50) not null comment '年级名称',
primary key (`gradeid`)
) engine=innodb auto_increment = 6 default charset = utf8;
-- 创建科目表
drop table if exists `subject`;
create table `subject`(
`subjectno`int(11) not null auto_increment comment '课程编号',
`subjectname` varchar(50) default null comment '课程名称',
`classhour` int(4) default null comment '学时',
`gradeid` int(4) default null comment '年级编号',
primary key (`subjectno`)
)engine = innodb auto_increment = 19 default charset = utf8;
-- 创建成绩表
drop table if exists `result`;
create table `result`(
`studentno` int(4) not null comment '学号',
`subjectno` int(4) not null comment '课程编号',
`examdate` datetime not null comment '考试日期',
`studentresult` int (4) not null comment '考试成绩',
key `subjectno` (`subjectno`)
)engine = innodb default charset = utf8;
-- 插入学生数据 其余自行添加 这里只添加了2行
insert into `student` (`studentno`,`loginpwd`,`studentname`,`sex`,`gradeid`,`phone`,`address`,`borndate`,`email`,`identitycard`)
values
(1000,'123456','张伟',0,2,'13800001234','北京朝阳','1980-1-1','[email protected]','123456198001011234'),
(1001,'123456','赵强',1,3,'13800002222','广东深圳','1990-1-1','[email protected]','123456199001011233');
-- 插入成绩数据 这里仅插入了一组,其余自行添加
insert into `result`(`studentno`,`subjectno`,`examdate`,`studentresult`)
values
(1000,1,'2013-11-11 16:00:00',85),
(1000,2,'2013-11-12 16:00:00',70),
(1000,3,'2013-11-11 09:00:00',68),
(1000,4,'2013-11-13 16:00:00',98),
(1000,5,'2013-11-14 16:00:00',58);
-- 插入年级数据
insert into `grade` (`gradeid`,`gradename`) values(1,'大一'),(2,'大二'),(3,'大三'),(4,'大四'),(5,'预科班');
-- 插入科目数据
insert into `subject`(`subjectno`,`subjectname`,`classhour`,`gradeid`)values
(1,'高等数学-1',110,1),
(2,'高等数学-2',110,2),
(3,'高等数学-3',100,3),
(4,'高等数学-4',130,4),
(5,'C语言-1',110,1),
(6,'C语言-2',110,2),
(7,'C语言-3',100,3),
(8,'C语言-4',130,4),
(9,'Java程序设计-1',110,1),
(10,'Java程序设计-2',110,2),
(11,'Java程序设计-3',100,3),
(12,'Java程序设计-4',130,4),
(13,'数据库结构-1',110,1),
(14,'数据库结构-2',110,2),
(15,'数据库结构-3',100,3),
(16,'数据库结构-4',130,4),
(17,'C#基础',130,1);
2.指定查询字段
2.1简单查询:
-- 查询全部的学生
SELECT * FROM `student`
-- 语法:SELECT 字段 FROM `表`
-- 查询指定字段
SELECT `studentno`,`studentname` FROM `student`
-- 别名,给结果起一个名字 AS 可以给字段起别名,也可以给表起别名
SELECT `studentno` AS '学号' , `studentname` AS '学生姓名' FROM `student`
-- 利用函数实现拼接 CONCAT(a,b,...)
SELECT CONCAT('学生姓名:',`studentname`) AS '拼接后' FROM `student`
语法: SELECT 字段,… FROM 表
总结:
- 有的时候,列名字不是那么的见名知意。我们可以起别名
- 利用AS起别名: 字段名 AS 别名 表名 AS 别名
2.2去重(DISTINCT)
作用:去除select语句中查询出来的结果中重复的数据,只显示一条
-- 查询一下有哪些同学参加了考试
SELECT * FROM `result` -- 查询全部的考试成绩
SELECT `studentno` FROM `result` -- 查询有哪些同学参加了考试
-- 发现重复数据,去重
SELECT DISTINCT `studentno` FROM `result`
2.3数据库的列(表达式)
-- 查询Mysql的版本(函数)
SELECT VERSION()
-- 用来计算(计算表达式)
SELECT 100*3-1 AS '计算结果'
-- 查询自增步长(变量)
SELECT @@auto_increment_increment
-- 查询学生考试成绩+1分查看
SELECT `studentno`,`studentresult`+2 AS '新成绩' FROM `result`
数据库中的表达式: 文本值,列,Null,函数,计算表达式,系统变量…
语法:select 表达式 from 表
3.where条件子句
作用:检索数据中符合条件的数据
搜索的条件由一个或者多个表达式组成!结果为布尔值
布尔值:是一个逻辑值,只有true or false
3.1逻辑运算符
尽量使用英文字母
-- 查询考试成绩在90~80分之间的数据
SELECT `studentno`,`studentresult` AS '70~80的成绩' FROM `result`
WHERE `studentresult` >= '70' AND `studentresult`<='80'
-- 注意,这里where条件用 AND或&& 进行连接
SELECT `studentno`,`studentresult` AS '70~80的成绩' FROM `result`
WHERE `studentresult` >= '70' && `studentresult`<='80'
-- 模糊查询(区间:BETWEEN ... AND ...)
SELECT `studentno`,`studentresult` AS '70~80的成绩' FROM `result`
WHERE `studentresult` BETWEEN 70 AND 80
-- 查询除了70分之外的成绩
SELECT `studentno`,`studentresult` AS '除了70分的成绩' FROM `result`
WHERE `studentresult` != '70'
-- != not
SELECT `studentno`,`studentresult` AS '除了70分的成绩' FROM `result`
WHERE NOT `studentresult` = '70'
-- 查询除了70分和85分之外的成绩
SELECT `studentno`,`studentresult` AS '除了70和85分的成绩' FROM `result`
WHERE NOT `studentresult`='70' AND NOT `studentresult`= '85'
3.2模糊查询:比较运算符
-- =====================模糊查询 ============================
-- 模糊查询:比较运算符
-- LIKE结合 % (代表0-任意个字符) _(代表一个字符)
SELECT `studentno`,`studentname` FROM `student`
WHERE studentname LIKE '张%'
-- 查询姓张的同学,名字后面只有一个字的
SELECT `studentno`,`studentname` AS '姓张的同学' FROM `student`
WHERE `studentname` LIKE '张_'
-- 查询姓张的同学,后面有两个字的
SELECT `studentno` AS '编号',`studentname` AS '3字姓张' FROM `student`
WHERE `studentname` LIKE '张__'
-- 查询名中间有小的同学(%小%)
SELECT `studentno`,`studentname` FROM `student`
WHERE `studentname` LIKE '%小%'
-- 查询在北京的学生
SELECT `studentno`,`studentname`,`address` FROM `student`
WHERE `address` LIKE ('%北京%')
-- ====== IN(具体的一个或者多个值) ========
-- 查询 1000,1001,1003号学员
SELECT `studentno`,`studentname` FROM `student`
WHERE `studentno` IN ('1000','1001','1003')
-- ============ NULL NOT NULL=====================
-- 查询地址为空的学员
SELECT `studentno`,`studentname`,`address` FROM `student`
WHERE `address` IS NULL OR `address` = ''
-- 没有为空的数据,插入一条为空的,注:为空和没有数据是两个概念
INSERT INTO `student`(`studentno`,`studentname`,`phone`,`address`,`email`)
VALUES ('1002','徐堰兵','18104131465','北凉','[email protected]')
-- 查询有生日信息的同学 (不为空)
SELECT `studentno`,`studentname`,`borndate` FROM `student`
WHERE `borndate` IS NOT NULL
3.3联表查询
联表查询的7种形式:
-- 联表查询 JOIN
-- 查询参加了考试的同学(学号,姓名,科目编号,分数)
SELECT * FROM student;
SELECT * FROM result;
/*
思路:
1.分析需求,分析查询的字段来自哪些表
2.确定使用哪种连接查询? 7种
确定交叉点 (这两个表的数据是相同的)
判断的条件: student 表中的 studentno = result 表中的 studentno
*/
-- 查询参加了考试的同学(学号,姓名,科目编号,分数)
SELECT s.studentno,studentname,subjectno,studentresult
FROM student s
INNER JOIN result r
WHERE s.studentno = r.studentno;
-- RIGHT JOIN
SELECT student.studentno,studentname,subjectno,studentresult
FROM student
RIGHT JOIN result
ON student.studentno = result.studentno;
-- LEFT JOIN
SELECT s.studentno,studentname,subjectno,studentresult
FROM student AS s
LEFT JOIN result AS r
ON s.studentno = r.studentno;
-- 查询缺考的同学
SELECT s.studentno,studentname,subjectno,studentresult
FROM student AS s
LEFT JOIN result AS r
ON s.studentno = r.studentno
WHERE studentresult IS NULL;
Inner join 、left join、right join 对比
思考题:
-- 思考题(查询了参加考试同学的信息:学号,学生姓名,科目名,分数,并按照分数进行排序。)
SELECT s.studentno,s.studentname,su.subjectname,r.studentresult
FROM student AS s
LEFT JOIN result AS r
ON s.studentno = r.studentno
LEFT JOIN `subject` AS su
ON r.subjectno=su.subjectno
WHERE studentresult IS NOT NULL
ORDER BY studentresult DESC;
/*
解题思路:
SELECT 字段名
FROM 表
xxx JOIN 连接表
ON 交叉条件
xxx JOIN 连接表
ON 交叉条件
WHERE 判断条件
ORDER BY 排序条件
*/
/*
理解 RIGHT JOIN、LEFT JOIN
FROM student RIGHT JOIN result 以result表为准
FROM student LEFT JOIN result 以student表为准
*/
标签:studentname,student,--,查询,studentno,联表,DQL,SELECT
From: https://www.cnblogs.com/itxiaofei/p/16990963.html