没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论


















高级查询在数据库中用得是最频繁的,也是应用最广泛的。
Ø 基本常用查询
--select
select * from student;
--all 查询所有
select all sex from student;
--distinct 过滤重复
select distinct sex from student;
--count 统计
select count(*) from student;
select count(sex) from student;
select count(distinct sex) from student;
--top 取前 N 条记录
select top 3 * from student;
--alias column name 列重命名
select id as 编号, name '名称', sex 性别 from student;
--alias table name 表重命名
select id, name, s.id, s.name from student s;
--column 列运算
select (age + id) col from student;
select s.name + '-' + c.name from classes c, student s where s.cid =
c.id;
--where 条件
select * from student where id = 2;
select * from student where id > 7;
select * from student where id < 3;
select * from student where id <> 3;
select * from student where id >= 3;
select * from student where id <= 5;
select * from student where id !> 3;
select * from student where id !< 5;
--and 并且

select * from student where id > 2 and sex = 1;
--or 或者
select * from student where id = 2 or sex = 1;
--between ... and ... 相当于并且
select * from student where id between 2 and 5;
select * from student where id not between 2 and 5;
--like 模糊查询
select * from student where name like '%a%';
select * from student where name like '%[a][o]%';
select * from student where name not like '%a%';
select * from student where name like 'ja%';
select * from student where name not like '%[j,n]%';
select * from student where name like '%[j,n,a]%';
select * from student where name like '%[^ja,as,on]%';
select * from student where name like '%[ja_on]%';
--in 子查询
select * from student where id in (1, 2);
--not in 不在其中
select * from student where id not in (1, 2);
--is null 是空
select * from student where age is null;
--is not null 不为空
select * from student where age is not null;
--order by 排序
select * from student order by name;
select * from student order by name desc;
select * from student order by name asc;
--group by 分组
按照年龄进行分组统计
select count(age), age from student group by age;
按照性别进行分组统计
select count(*), sex from student group by sex;
按照年龄和性别组合分组统计,并排序
select count(*), sex from student group by sex, age order by age;
按照性别分组,并且是 id 大于 2 的记录最后按照性别排序

select count(*), sex from student where id > 2 group by sex order by
sex;
查询 id 大于 2 的数据,并完成运算后的结果进行分组和排序
select count(*), (sex * id) new from student where id > 2 group by
sex * id order by sex * id;
--group by all 所有分组
按照年龄分组,是所有的年龄
select count(*), age from student group by all age;
--having 分组过滤条件
按照年龄分组,过滤年龄为空的数据,并且统计分组的条数和现实年龄信息
select count(*), age from student group by age having age is not
null;
按照年龄和 cid 组合分组,过滤条件是 cid 大于 1 的记录
select count(*), cid, sex from student group by cid, sex having cid >
1;
按照年龄分组,过滤条件是分组后的记录条数大于等于 2
select count(*), age from student group by age having count(age) >=
2;
按照 cid 和性别组合分组,过滤条件是 cid 大于 1,cid 的最大值大于 2
select count(*), cid, sex from student group by cid, sex having cid >
1 and max(cid) > 2;
Ø 嵌套子查询
子查询是一个嵌套在 select、insert、update 或 delete 语句或其他子查询中的查询。任
何允许使用表达式的地方都可以使用子查询。子查询也称为内部查询或内部选择,而包含子查
询的语句也成为外部查询或外部选择。
# from (select … table)示例
将一个 table 的查询结果当做一个新表进行查询
select * from (
select id, name from student where sex = 1
) t where t.id > 2;
上面括号中的语句,就是子查询语句(内部查询)。在外面的是外部查询,其中外部查询可以
包含以下语句:
剩余16页未读,继续阅读
资源评论


aliang0113
- 粉丝: 0
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 基于物联网技术的垃圾桶智能管理系统设计与实现.doc
- 全国自考C加加程序设计试题.doc
- 计算机教育中计算机科学技术的运用探讨.docx
- (源码)基于Arduino的ITS150遥控器模拟器.zip
- 电子商务教研计划.doc
- 江西省中小学安全知识网络答题活动答案解析.doc
- Web前端技术课程实训分析报告.doc
- 电子商务网站盈利能力的理性分析.doc
- 移动互联网环境下混合式教学设计与实践.docx
- 教育系统安全大检查市级督查巡查工作记录单.docx
- 计算机网络安全技术实验四.doc
- AVR单片机的通信系统设计方案.doc
- 略谈工程项目管理中材料成本控制的难点及对策.docx
- 个人网络信息安全防范.doc
- 基于大数据时代下档案管理工作存在的问题与对策研究.docx
- (源码)基于Arduino的MPU9250陀螺仪运动处理单元俯仰角控制项目.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制
