当前位置:网站首页>第七章 数据查询
第七章 数据查询
2022-08-06 05:18:00 【ee .】
第七章 数据查询
1.掌握select 查询语句的基本格式
select 字段 from 表名 where 条件
group by 分组
having 条件
order by 排序dec | desc
limit 限制查询行数
into outfile 保存查询结果;
2.掌握基本的无条件查询
select * from 表名;
3.掌握条件查询的使用
select * from xsjbxxb where xb='女';
别名设置:select 字段1 as 别名 from xsjbxxb;
eg:select bjbh as 班级编号 from xsjbxxb;
带in关键字查询:select xh,kcdm from xsxkb where in('00202117','00202118');
带between and 关键字查询:select xh,kcdm,cj from xsxkb where cj between 80 and 90;
带like 的模糊查询:select xh,xm from xsjbxxb where xm like '张%';
带is null 的空查询:select * from xsxkb where cj is null;
带and | or的多条件查询:select xh,xm,xb,csrq from xsjbxxb where xb='男' and csrq>='2000-1-1';
4.掌握使用聚合函数实现数据的统计
常用聚合函数:count,sum,avg,max,min
count: select count(*) as 学生人数 from xsjbxxb;
sum: select sum(cj) as 总成绩 from xsxkb where xh='201820107101';
avg: select avg(cj) as 平均成绩 from xsxkb where xh='201820107101';
5.掌握分组和排序查询的使用
分组查询(和聚合函数配合使用):注意select 查询的数据只能是group by分组的字段,或使用了聚合函数的非分组字段。
group by <组合表达式> [with rollup] [having where_condition]; 提示:with rollup 表示对分组的数据进行分类汇总。
select xb as 性别,count(*) as 人数
from xsjbxxb
group by 1;
select xsxkb.xh as 学号,count(*) as 选课门数
from xsxkb,xsjbxxb
where xsxkb.xh=xsjbxxb.xh
and xb='男'
group by xsxkb.xh
having count(*)>=3;
order by 排序查询,limit限制查询行数
order by <组合表达式> [asc | desc];
select xh,xm,xb,csrq
from xsjbxxb
where xb='女'
order by csrq desc
limit 3;
6.掌握多表查询的使用
内连接:只返回满足连接条件的数据行(join ... on ...)
select xs.xh,xm,kcmc,cj
from xsjbxxb as xs join xsxkb
on xs.xh=xsxkb.xh
join kcdmb on xsxkb.kcdm=kcdmb.kcdm
where xs.xh='201820107101';
外连接:
左外连接:结果集包括满足条件的行外,还包括左表中不满足条件的记录。
select xs.xh,xm,kcdm,cj
from xsjbxxb as xs left join xsxkb
on xs.xh=xsxkb.xh
where xs.xh='201820901102';
右外连接:结果集包括满足条件的行外,还包括右表中不满足条件的记录。(right join ......on......)
select xs.xh,kcdm,cj
from xsxkb right join xsjbxxb as xs
on xsxkb.xh=xs.xh
where xb='女';
交叉连接:产生笛卡尔积。(cross ..... join.....)
select *
from bmdmb cross join jsjbxxb;
自然连接:就是一个表的两个副本之间的连接(同一个表在from子句中出现两次,为了区分必须对表指定不同的别名)
查询和魏志强同专业的学生。
select xs1.xh,xs1.xm
from xsjbxxb as xs1,xsjbxxb as xs2
where xs1.zymc=xs2.zymc and xs2.xm='魏志强'
and xs1.xm!='魏志强';
7.掌握子查询的使用
答:子查询就是在一个查询语句中,还包括另一个查询语句。在外一层的查询中使用里面一层查询产生的结果集。它可以嵌套在select,insert,update,delete等语句或
其他子查询中。
比较子查询:select xh,cj
from xsxkb
where kcdm=(select kcdm from kcdmb where kcmc='微机原理与应用');
带in的关键子查询:select xh,xm
from xsjbxxb
where xh in(select xh from xsxkb where cj>90);
批量比较子查询:all:指定表达式要与子查询结果集中的每个值进行比较,只有当表达式的值都满足比较关系时,才能返回true。
some ,any:只要与子查询结果集中的某个值满足比较关系时,就会返回true。
select xm from xsjbxxb
where xh=some(select xh from xsxkb where cj <60);
exists子查询:该子查询不需要返回任何数据,只是返回一个逻辑值。当内层语句查询到满足条件的记录时,就返回一个真值,否则返回一个假值。
select xm
from xsjbxxb
where exists(select * from xsxkb where xsjbxxb.xh=xsxkb.xh);
利用子查询插入,更新,删除。
插入: create table xs like xsjbxxb;创建结构与xsjbxxb一致的表。
insert into xs
select * from xsjbxxb
where year(csrq)=2000;
更新: update xs
set jl='优秀'
where xh in(select xh from xsxkb where cj>90);
删除: delete from xs
where xh in(select xh from xsxkb where cj<60);
8.掌握正则表达式的模糊查询
答:常用字符:^,$,.,*,+,<字符串>,[字符集合],[a-z],[^],字符串{n},字符串{n,}字符串{m,n}.
语法:where 字段名 [not] [regexp | rlike] 表达式;
eg:select xh,xm,xb
from xsjbxxb
where xm regexp'^张%';
补充:合并结果集(union)
select bjbh,bmh,bjzwmc
from bjdmb
where bmh='01'
union
select bjbh,bmh,bjzwmc
from bjdmb
where bmh='03';
边栏推荐
- A small test of basic grammar, Go lang1.18 introductory refining tutorial, from Bai Ding to Hongru, basic grammar of go lang and the use of variables EP02
- Environmental sensitivity/thermal NMR/auxin-loaded/membrane-loaded/protein-loaded hydrogels
- The web vulnerability scanner - appscan
- 2022年国外受欢迎的海外社交平台排行榜!
- 阿里怎么用DDD来拆分微服务?
- Matlab Implementation of Maximum Entropy Theorem for Discrete Sources
- mfc多文档创建新的视图
- Minesweeper (C language)
- Docker基础(七)—安装mysql服务
- Develop a SQL editor with Monaco Editor
猜你喜欢

一台电脑网线连接另一台电脑,通过另一台电脑wifi网络上网

Oracle Recovery Tools 解决ORA-600 3020故障

如何保持注意力,高效学习

调用openssl api函数C代码生成证书

环境敏感性/温核磁共振/载生长素/载细胞膜/载蛋白质水凝胶的研究

Environmental sensitivity/thermal NMR/auxin-loaded/membrane-loaded/protein-loaded hydrogels

linux的centos免密登录mysql数据库(mysql忘记密码情况)

把任意网站变成桌面软件

数组相关方法使用笔记

基于 DDD 的微服务设计和开发实战
随机推荐
Learn and understand architecture design from business development
金属材料/多肽/多糖/化合物/抗体/量子点/黑磷量子点修饰水凝胶
atoll C语言实现
php向mysql写入数据失败
调用openssl api函数C代码生成证书
ACC编程应用挑战赛决赛真题
一文彻底解析数据库设计思路
SSM整合进阶版
fegin feign.FeignException: status 404 reading StudentService#getAll()
C language advanced articles: custom types--bit segment, enumeration and union
移动端学习
php fails to write data to mysql
记录第一篇博客
How to avoid the account risk of cross-border e-commerce risk?What is the solution?
1403. 非递增顺序的最小子序列
线性筛详解(严密证明)+代码
Oracle Recovery Tools to solve ORA-600 3020 failure
ES6中Array的常用对象方法
There are benefits at the end of the article | What adb commands have you mastered commonly used in App testing?
从业务开发中学习和理解架构设计