select 查询
使用as给字段起别名,例如:select name as 姓名 from student;
模糊匹配(like)
"_":一个占位符。例子:select * from student where name like "_ack"; // 表示匹配name以"ack"结尾,且为四个字符的值。
"%":匹配0个或多个任意字符。
关于null的查询:
null在数据库中不代表没有,而是代表不知道。
select name from student where name=null; // 这句是错误的,理解起来就是:从student中查询name等于"不知道的值",这就无法查了
select name from student where name<>null; // 这句也是错误的。理解起来是:从student中查询name不等于"不知道的值"。这也无法查。
select name from student where name is null; // 这句话是正确的,理解起来是:从student中查询name是"不知道的值"
范围选取(between...and...)
select age from student where age between 10 and 20;
等价于:
select age from student where age>=10 and age<=20;
聚合函数:
AVG():求平均值。
Count():计算总数。
All():默认值,表示全部。
Max():最大值。
...
分组Group By:
select age,Count(*) from student Group By age; // 根据年龄进行分组,并统计各个年龄的人数。
//
// 值得注意的是,聚合函数不能出现在where子句中
select age,Count(*) from student where Count(*)>1 Group By age;
// 如果需要筛选,可以使用Having来进行过滤。
select age,Count(*) from student Group By age Having Count(*)>1;
//
//
// where不等价于group by,因为group by是对选出来的结果进行过滤的。下面进行列子说明
select age from student where Score>90 Group By age; // 理解为:从student表中选择Score>90的age,再将age进行分组
查找前几行(top):
select top 3 * from name order by age DESC; // 根据age进行降序排序查找前3行
不重复(distinct):
select distinct name from student; // 选取student表中不重复的name
合并显示(union),默认将重复的结果去除:
(select name from student) union (select name from teacher); // 表示将两个表的结果合并显示。
// union all 可以将两个或者两个以上的表的结果合并显示,默认不会将重复结果去除,也就是查询全部的意思。
// 使用union进行关联时,关联的结果字段数量要相同,字段类型要相同。
数字函数
ABS():求绝对值。
CEILING():舍入到最大整数。
FLOOR():舍入到最小整数。
ROUND():四舍五入。
字符串函数
len():计算字符串长度。
lower():转换为小写。
upper():转换为大写。
ltrim():去除左边空格。
rtrim():去除右边空格。
substring():字符串截取。substring(被截取的字符串,字符串起始位置,字符串长度)
日期函数
getdate():获取日期
dateadd():增加以后的日期。例子:DATEADD(year,8,getdate()),表示在参数getdate()的基础上增加8年(在当前时间加上8年)。单位可以用year/month/day/hh(小时)/...等。
datediff():计算两个日期之间的差额。dateiff(单位,开始日期,结束日期),单位同上有yaer等
datepart():返回日期的部分。datepart(单位,日期参数)。单位同上;日期参数,如当前日期参数:getdate() 案列:
--select dateadd(dd,-day(getdate())+1,getdate()); -- 获取本月第一天
--select dateadd(ms,-3,dateadd(mm,datediff(m,0,getdate())+1,0))
--select getdate() -- 获取当前时间
--select day(getdate()) -- 获取是当前月的第几天
select dateadd(dd,-day(getdate())+1,getdate()) -- 获取本月第一天
---------------------------------
--select datediff(m,0,getdate()) -- 第二个0表示1900-01-01。计算与1900年相差的月份,月份向下取整。例如今天是2019-03-05 13:50:45,那么获取到的值为:2019-03-01那一天以前的月份(也就是不包含3月份)
--select dateadd(mm,datediff(m,0,getdate())+1,0) -- 第三个参数的0表示1900-01-01.表示相差月份加上1900-01-01得到的日期,往往是下个月的第一天,如今天是2019-03-05 13:50:45,那么获取到的值为:2019-04-01 00:00:00.000
select dateadd(ms,-3,dateadd(mm,datediff(m,0,getdate())+1,0)) -- 在上面的基础上减去3毫秒。表示取当前月的最后一天。
day(dateadd(ms,-3,dateadd(mm,datediff(m,0,getdate())+1,0))) -- 当前月天数
---------------------------------
select datediff(mm,'2018-2-15','2018-3-14'); -- 获取相差月份,这里显示1
类型转换
cast():例子 cast('123' as int) // str转int
convert():例子 convert(datetime, '2019-2-3') // str转datetime
空值处理函数(isnull):
select isnull(name,'jr') from student; // 表示从student表中选取name字段,如果字段为空,那么用'jr'代替。
case函数用法:
select name, (
case age
when 10 then '少年'
when 25 then '青年'
when 50 then '中年'
else '不在检查年龄'
end
) as 年纪类型 from student;
//
// 上面看起来并不完整,应该进行年龄段的判断。
select name, (
case
when age<=10 then '少年'
when age>10 and age<=25 then '青年'
when age>25 and age<=50 then '中年'
else '不在检查年龄'
end
) as 年纪类型 from student;
//
// 那么如果要计算年龄类型的数量呢?
select sex, SUM(
case
when age<=10 then 1
else 0
end
) as 少年, SUM(
case
when age>10 and age<=25 then 1
else 0
end
) as 青年, SUM(
case
when age>25 and age<=50 then 1
else 0
end
) as 中年
from student group by sex;

关联查询:
select stu.name,tea.name from student as stu join teacher as tea on stu.id=tea.sid; // 使用join
select student.name, teacher.name from student, teacher where student.id=teacher.sid;
select 查询的更多相关文章
- 关于SubSonic3.0插件使用SqlQuery或Select查询时产生的System.NullReferenceException异常修复
早上在编写执行用例时,突然爆异常System.NullReferenceException: 未将对象引用设置到对象的实例 执行代码:
- 为什么忘记commit也会造成select查询的性能问题
今天遇到一个很有意思的问题,一个开发人员反馈在测试服务器ORACLE数据库执行的一条简单SQL语句非常缓慢,他写的一个SQL没有返回任何数据,但是耗费了几分钟的时间.让我检查分析一下原因,分析解决过后 ...
- [NHibernate]N+1 Select查询问题分析
目录 写在前面 文档与系列文章 N+1 Select查询问题分析 总结 写在前面 在前面的文章(延迟加载,立即加载)中都提到了N+1 Select的问题,总觉得理解的很不到位,也请大家原谅,这也是为什 ...
- access数据库select查询top时无效的解决办法
access数据库select查询top时有时无效,原因就是在使用Order by时,且排序的条件中数据有重复的. 比如:select top 10 * from table1 order by cd ...
- MySQL之select查询、function函数
一.select查询 //查询某张表所有数据 select * from temp; //查询指定列和条件的数据 //查询name和age这两列,age等于22的数据 ; //as对列重命名 //as ...
- select查询的性能
为什么忘记commit也会造成select查询的性能问题 今天遇到一个很有意思的问题,一个开发人员反馈在测试服务器ORACLE数据库执行的一条简单SQL语句非常缓慢,他写的一个SQL没有返回任何数据, ...
- 把一个select查询结果插入到一个表(可选指定字段和值实例)
把一个select查询结果插入到一个表(可选指定字段和值实例) insert into bak (cc,yf) select cc,9 from ket insert into bak (cc,yf ...
- Sql Server 函数的操作实例!(执行多条语句,返回Select查询后的临时表)
Sql Server 函数的操作实例!(执行多条语句,返回Select查询后的临时表) SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ==== ...
- select查询原理
原文:select查询原理 我并非专业DBA,但做为B/S架构的开发人员,总是离不开数据库,一般开发员只会应用SQL的四条经典语句:select ,insert,delete,update.但是我从来 ...
- mybatis mapper.xml 写关联查询 运用 resultmap 结果集中 用 association 关联其他表 并且 用 association 的 select 查询值 报错 java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for mybatis.map
用mybaits 写一个关联查询 查询商品表关联商品规格表,并查询规格表中的数量.价格等,为了sql重用性,利用 association 节点 查询 结果并赋值报错 商品表的mapper文件为Gooo ...
随机推荐
- Python自学:第三章 修改列表元素
motorcycles = ['honda', 'yamaha', 'suzuki'] print(motorcycles) motorcycles[0] = 'ducati' print(motor ...
- CodeForces - 363D --二分和贪心
题目:CodeForces - 363D 题意:给定n个学生,其中每个学生都有各自的私己钱,并且自己的私己钱只能用在自己买自行车,不能给别人. 给定m个自行车,每个自行车都有一个价格. 给定公有财产a ...
- 『TensorFlow』读书笔记_VGGNet
VGGNet网络介绍 VGG系列结构图, 『cs231n』卷积神经网络工程实践技巧_下 1,全部使用3*3的卷积核和2*2的池化核,通过不断加深网络结构来提升性能. 所有卷积层都是同样大小的filte ...
- 高级FTP服务器开发
要求: 1. 用户加密认证 2. 多用户同时登陆 3. 每个用户有自己的家目录且只能访问自己的家目录 4. 对用户进行磁盘配额.不同用户配额可不同 5. 用户可以登陆server后,可切换目录 6. ...
- js中的setTimeout第三个参数
setTimeout跟setInterval大家应该都很熟悉的,但是一直很少注意,原来这两个函数可以支持第三个参数的,但是IE就呵呵了,仅IE(6-9)呵呵了,其他浏览器都支持的。 第三个参数将作为回 ...
- UVa Live 4794 - Sharing Chocolate 枚举子集substa = (s - 1) & substa,记忆化搜索 难度: 2
题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...
- 老司机浅谈linux系统学习技巧
Linux起源于20世纪70年代,是一种优秀的操作系统系统.初次接触到linux这个系统是在大学期间,这样才发现除了windows外的另外一个有趣系统.开始抱着好奇的心态去了解,随着深入学习,笔者被它 ...
- redis主从简单配置
网上有好多复杂的配置,这里我用的是windows版的redis,简单配置了下,试验了下主从,能正常使用. 1.redis-master文件夹(里面是redis),redis-slave文件夹(里面是r ...
- mysql InnoDB index 主键采用聚簇索引,二级索引不采用聚簇索引
原文链接 我的归纳: (1)InnoDB的主键采用聚簇索引存储,使用的是B+Tree作为索引结构,但是叶子节点存储的是索引值和数据本身(注意和MyISAM的不同). (2)InnoDB的二级索引不使用 ...
- leetcode python 007
## 翻转整数def evert(int0): if int0<0: flg=1 else: flg=0 e=int(str(int0)[flg: ...