使用dql查询数据
dql语言dql( data query language 数据查询语言 )
查询数据库数据 , 如select语句
简单的单表查询或多表的复杂查询和嵌套查询
是数据库语言中最核心,最重要的语句
使用频率最高的语句
select语法
select [all | distinct]{* | table.* | [table.field1[as alias1][,table.field2[as alias2]][,...]]}from table_name [as table_alias] [left | right | inner join table_name2] -- 联合查询 [where ...] -- 指定结果需满足的条件 [group by ...] -- 指定结果按照哪几个字段来分组 [having] -- 过滤分组的记录必须满足的次要条件 [order by ...] -- 指定查询记录按一个或多个条件排序 [limit {[offset,]row_count | row_countoffset offset}]; -- 指定查询的记录从哪条至哪条
注意 : [ ] 括号代表可选的 , { }括号代表必选得
指定查询字段-- 查询表中所有的数据列结果 , 采用 **" \* "** 符号; 但是效率低,不推荐 .-- 查询所有学生信息select * from student;-- 查询指定列(学号 , 姓名)select studentno,studentname from student;
as 子句作为别名
作用:
可给数据列取一个新别名
可给表取一个新别名
可把经计算或总结的结果用另一个新名称来代替
-- 这里是为列取别名(当然as关键词可以省略)select studentno as 学号,studentname as 姓名 from student;-- 使用as也可以为表取别名select studentno as 学号,studentname as 姓名 from student as s;-- 使用as,为查询结果取一个新名字-- concat()函数拼接字符串select concat('姓名:',studentname) as 新姓名 from student;
distinct关键字的使用
作用 : 去掉select查询返回的记录结果中重复的记录 ( 返回所有列的值都相同 ) , 只返回一条
-- # 查看哪些同学参加了考试(学号) 去除重复项select * from result; -- 查看考试成绩select studentno from result; -- 查看哪些同学参加了考试select distinct studentno from result; -- 了解:distinct 去除重复项 , (默认是all)
使用表达式的列
数据库中的表达式 : 一般由文本值 , 列值 , null , 函数和操作符等组成
应用场景 :
select语句返回结果列中使用
select语句中的order by , having等子句中使用
dml语句中的 where 条件语句中使用表达式
-- selcet查询中可以使用表达式select @@auto_increment_increment; -- 查询自增步长select version(); -- 查询版本号select 100*3-1 as 计算结果; -- 表达式-- 学员考试成绩集体提分一分查看select studentno,studentresult+1 as '提分后' from result;
避免sql返回结果中包含 ' . ' , ' * ' 和括号等干扰开发语言程序.
where条件语句作用:用于检索数据表中 符合条件 的记录
搜索条件可由一个或多个逻辑表达式组成 , 结果一般为真或假.
逻辑操作符
测试
-- 满足条件的查询(where)select studentno,studentresult from result;-- 查询考试成绩在95-100之间的select studentno,studentresultfrom resultwhere studentresult>=95 and studentresult<=100;-- and也可以写成 &&select studentno,studentresultfrom resultwhere studentresult>=95 && studentresult<=100;-- 模糊查询(对应的词:精确查询)select studentno,studentresultfrom resultwhere studentresult between 95 and 100;-- 除了1000号同学,要其他同学的成绩select studentno,studentresultfrom resultwhere studentno!=1000;-- 使用notselect studentno,studentresultfrom resultwhere not studentno=1000;
模糊查询 :比较操作符
注意:
数值数据类型的记录之间才能进行算术运算 ;
相同数据类型的数据之间才能进行比较 ;
测试:
-- 模糊查询 between and \ like \ in \ null-- =============================================-- like-- =============================================-- 查询姓刘的同学的学号及姓名-- like结合使用的通配符 : % (代表0到任意个字符) _ (一个字符)select studentno,studentname from studentwhere studentname like '刘%';-- 查询姓刘的同学,后面只有一个字的select studentno,studentname from studentwhere studentname like '刘_';-- 查询姓刘的同学,后面只有两个字的select studentno,studentname from studentwhere studentname like '刘__';-- 查询姓名中含有 嘉 字的select studentno,studentname from studentwhere studentname like '%嘉%';-- 查询姓名中含有特殊字符的需要使用转义符号 '\'-- 自定义转义符关键字: escape ':'-- =============================================-- in-- =============================================-- 查询学号为1000,1001,1002的学生姓名select studentno,studentname from studentwhere studentno in (1000,1001,1002);-- 查询地址在北京,南京,河南洛阳的学生select studentno,studentname,address from studentwhere address in ('北京','南京','河南洛阳');-- =============================================-- null 空-- =============================================-- 查询出生日期没有填写的同学-- 不能直接写=null , 这是代表错误的 , 用 is nullselect studentname from studentwhere borndate is null;-- 查询出生日期填写的同学select studentname from studentwhere borndate is not null;-- 查询没有写家庭住址的同学(空字符串不等于null)select studentname from studentwhere address='' or address is null;
连接查询join 对比
七种join:
测试
/*连接查询 如需要多张数据表的数据进行查询,则可通过连接运算符实现多个查询内连接 inner join 查询两个表中的结果集中的交集外连接 outer join 左外连接 left join (以左表作为基准,右边表来一一匹配,匹配不上的,返回左表的记录,右表以null填充) 右外连接 right join (以右表作为基准,左边表来一一匹配,匹配不上的,返回右表的记录,左表以null填充) 等值连接和非等值连接自连接*/-- 查询参加了考试的同学信息(学号,学生姓名,科目编号,分数)select * from student;select * from result;/*思路:(1):分析需求,确定查询的列来源于两个类,student result,连接查询(2):确定使用哪种连接查询?(内连接)*/select s.studentno,studentname,subjectno,studentresultfrom student sinner join result ron r.studentno = s.studentno-- 右连接(也可实现)select s.studentno,studentname,subjectno,studentresultfrom student sright join result ron r.studentno = s.studentno-- 等值连接select s.studentno,studentname,subjectno,studentresultfrom student s , result rwhere r.studentno = s.studentno-- 左连接 (查询了所有同学,不考试的也会查出来)select s.studentno,studentname,subjectno,studentresultfrom student sleft join result ron r.studentno = s.studentno-- 查一下缺考的同学(左连接应用场景)select s.studentno,studentname,subjectno,studentresultfrom student sleft join result ron r.studentno = s.studentnowhere studentresult is null-- 思考题:查询参加了考试的同学信息(学号,学生姓名,科目名,分数)select s.studentno,studentname,subjectname,studentresultfrom student sinner join result ron r.studentno = s.studentnoinner join `subject` subon sub.subjectno = r.subjectno
自连接
/*自连接 数据表与自身进行连接需求:从一个包含栏目id , 栏目名称和父栏目id的表中 查询父栏目名称和其他子栏目名称*/-- 创建一个表create table `category` ( `categoryid` int(10) unsigned not null auto_increment comment '主题id', `pid` int(10) not null comment '父id', `categoryname` varchar(50) not null comment '主题名字', primary key (`categoryid`)) engine=innodb auto_increment=9 default charset=utf8-- 插入数据insert into `category` (`categoryid`, `pid`, `categoryname`)values('2','1','信息技术'),('3','1','软件开发'),('4','3','数据库'),('5','1','美术设计'),('6','3','web开发'),('7','5','ps技术'),('8','2','办公信息');-- 编写sql语句,将栏目的父子关系呈现出来 (父栏目名称,子栏目名称)-- 核心思想:把一张表看成两张一模一样的表,然后将这两张表连接查询(自连接)select a.categoryname as '父栏目',b.categoryname as '子栏目'from category as a,category as bwhere a.`categoryid`=b.`pid`-- 思考题:查询参加了考试的同学信息(学号,学生姓名,科目名,分数)select s.studentno,studentname,subjectname,studentresultfrom student sinner join result ron r.studentno = s.studentnoinner join `subject` subon sub.subjectno = r.subjectno-- 查询学员及所属的年级(学号,学生姓名,年级名)select studentno as 学号,studentname as 学生姓名,gradename as 年级名称from student sinner join grade gon s.`gradeid` = g.`gradeid`-- 查询科目及所属的年级(科目名称,年级名称)select subjectname as 科目名称,gradename as 年级名称from subject subinner join grade gon sub.gradeid = g.gradeid-- 查询 数据库结构-1 的所有考试结果(学号 学生姓名 科目名称 成绩)select s.studentno,studentname,subjectname,studentresultfrom student sinner join result ron r.studentno = s.studentnoinner join `subject` subon r.subjectno = sub.subjectnowhere subjectname='数据库结构-1'
排序和分页测试
/*============== 排序 ================语法 : order by order by 语句用于根据指定的列对结果集进行排序。 order by 语句默认按照asc升序对记录进行排序。 如果您希望按照降序对记录进行排序,可以使用 desc 关键字。 */-- 查询 数据库结构-1 的所有考试结果(学号 学生姓名 科目名称 成绩)-- 按成绩降序排序select s.studentno,studentname,subjectname,studentresultfrom student sinner join result ron r.studentno = s.studentnoinner join `subject` subon r.subjectno = sub.subjectnowhere subjectname='数据库结构-1'order by studentresult desc/*============== 分页 ================语法 : select * from table limit [offset,] rows | rows offset offset好处 : (用户体验,网络传输,查询压力)推导: 第一页 : limit 0,5 第二页 : limit 5,5 第三页 : limit 10,5 ...... 第n页 : limit (pageno-1)*pageszie,pageszie [pageno:页码,pagesize:单页面显示条数] */-- 每页显示5条数据select s.studentno,studentname,subjectname,studentresultfrom student sinner join result ron r.studentno = s.studentnoinner join `subject` subon r.subjectno = sub.subjectnowhere subjectname='数据库结构-1'order by studentresult desc , studentnolimit 0,5-- 查询 java第一学年 课程成绩前10名并且分数大于80的学生信息(学号,姓名,课程名,分数)select s.studentno,studentname,subjectname,studentresultfrom student sinner join result ron r.studentno = s.studentnoinner join `subject` subon r.subjectno = sub.subjectnowhere subjectname='java第一学年'order by studentresult desclimit 0,10
子查询/*============== 子查询 ================什么是子查询? 在查询语句中的where条件子句中,又嵌套了另一个查询语句 嵌套查询可由多个子查询组成,求解的方式是由里及外; 子查询返回的结果一般都是集合,故而建议使用in关键字;*/-- 查询 数据库结构-1 的所有考试结果(学号,科目编号,成绩),并且成绩降序排列-- 方法一:使用连接查询select studentno,r.subjectno,studentresultfrom result rinner join `subject` subon r.`subjectno`=sub.`subjectno`where subjectname = '数据库结构-1'order by studentresult desc;-- 方法二:使用子查询(执行顺序:由里及外)select studentno,subjectno,studentresultfrom resultwhere subjectno=( select subjectno from `subject` where subjectname = '数据库结构-1')order by studentresult desc;-- 查询课程为 高等数学-2 且分数不小于80分的学生的学号和姓名-- 方法一:使用连接查询select s.studentno,studentnamefrom student sinner join result ron s.`studentno` = r.`studentno`inner join `subject` subon sub.`subjectno` = r.`subjectno`where subjectname = '高等数学-2' and studentresult>=80-- 方法二:使用连接查询+子查询-- 分数不小于80分的学生的学号和姓名select r.studentno,studentname from student sinner join result r on s.`studentno`=r.`studentno`where studentresult>=80-- 在上面sql基础上,添加需求:课程为 高等数学-2select r.studentno,studentname from student sinner join result r on s.`studentno`=r.`studentno`where studentresult>=80 and subjectno=( select subjectno from `subject` where subjectname = '高等数学-2')-- 方法三:使用子查询-- 分步写简单sql语句,然后将其嵌套起来select studentno,studentname from student where studentno in( select studentno from result where studentresult>=80 and subjectno=( select subjectno from `subject` where subjectname = '高等数学-2' ))/*练习题目: 查 c语言-1 的前5名学生的成绩信息(学号,姓名,分数) 使用子查询,查询郭靖同学所在的年级名称*/
相关推荐:《mysql教程》
以上就是如何使用dql查询数据的详细内容。