转载请声明出处 https://blog.youkuaiyun.com/cyzhah/article/details/82081707
一、为查询结果集中的列起别名(示例)(给小白的提示:你的数据库里面必须存在以下的表才能运行成功哦,运行环境SQLServer 2008)
1、select tsid as 学生编号,tsname as 学生姓名,tsgender as 性别 from TbStudent
(从TbStudent表中查询tsid,tsname,tsgender,并为他们起别名)
2、select 学生编号 = tsid,学生姓名 = tsname,性别 = tsgender, 婚否 = ‘否’ from TbStudent
(与上面的一句功能一样,唯一不同的是新增加了一列婚否的数据,并且全部都赋值为否)
二、数据检索top和distinct去重,排序(示例)
1、select distinct * from TblStudent
(上面即使表上存在重复也不会去重成功,因为distinct关键字是针对已经查询出的整个结果集然后去除重复)
select distinct tsname , tsgender , tsaddress from TbStudent(去重成功)
2、排序(Top一般会和排序一起使用 )
排序(order by 列名)(注 :order by 语句必须放在整个SQL语句的最后)
select * from TblScore order by Englisih desc,Math desc(先根据英语成绩排序,当英语成绩相同的时候在根据数学成绩排序)
select * from TbStudent order by age desc(按照年龄tsage降序排序)
select * , 平均分 = (English + Math)*1.0/2 from TblScore( 这里SQL语句在表里加了一个平均分的字段,原来不存在这个字段)
select * , 平均分 = (English + Math)*1.0/2 from TblScore desc (以平均分进行降序排序)
select * from TbStudent order by age asc (默认就是升序排序select * from TbStudent order by tsage )
select top 5 * from TbStudent order by age desc(取年龄最大的前5个)
select top 5 * from TbStudent order by age asc( 取年龄最小的前5个)
top 后面可以跟着表达式,但必须用括号括起来 如:(select top (6*6) * from TbStudent order by tsage asc)
select top 30 percent * from TblScore order by tMath desc
(30 percent 是取数学成绩排序后的前30%条,注意哦,如果数据是10条,30%会取到3条,如果是31%会取到4条的哦)
三、聚合函数
聚合函数是分组后用才有意义,是对数据进行简单的汇总
聚合函数默认会把整个表中的数据作为一组进行统计
select sum(age) as 年龄总和 from NewPerson(统计出所有人的年龄总和)
select count(*) from NewPerson(统计表中一共有多少条记录)
给个示例让大家看得明白点,假如我要计算平均年龄如下:
select 平均年龄 = (select sum(age) as 年龄总和 from NewPerson) / (select count(*) from NewPerson)
select max (age) from NewPerson (求出年龄最大的)
select min (age ) from NewPerson(求出年龄最小的)
select avg(age*1.0) from NewPerson(计算年龄的平均值)
select count(age) from TblStudent(聚合函数count()不统计空值null)
select avg(age) from TblStudent(聚合函数avg()不统计null)
四、查询
1、条件查询
select * from TblSocre where English <60 or Math<60(查询英语成绩小于60或者数学...)
select * from MyStudent where age >=20 and age<=30 and gender = '男'
select * from MyStudent where age between 20 and 30 and gender = '男';(与上面一句一样的效果)
select * from TblStudent where tsclassid = 3 or tsclassid = 4 or tsclassid = 5(查询出tsclassid 是 3,4 5 的)
select * from TblStudent where tsclassid in (3,4,5) (与上面一句一样的效果)
select * from TblStudent where tsclassid >=3 and tsclassid<=5(与上两句一样,建议使用这样的写法或(between 3 and 5 ))
select * from TbStudent where tsage<>25 (查询所有年龄不等于25的值)
2、模糊查询(针对字符串)
通配符:
_ (表示任意的单个字符)
select * from Student where name like ' 张_ ' (查询名字姓张的两个字的名字如 :张三)
select * from Student where name like '张__' (查询名字姓张的三个字的名字)
select * from Student where name like '张%' and len(name) = 2 (同上)
select * from Student where name like '张%' (无论名字的字数姓张的都取出来)
[ ](表示筛选,范围)
select * from Student where name like '张[0-9]三'
select * from Student where name like '张[a-z]三'
select * from Student where name like '张[a-z0-9]三'
select * from Student where name like '张[^0-9]三'
select * from Student where name not like '张[0-9]三' (查询除了张[0-9]三的数据)
select * from TblStudent where name like '%[%]% ' (查询姓名中存在%的名字)
select * from TblStudent where name like '% / ] % ' ESCAPE ' / ' (自己指定一个转义符)
select * from TblStudent where name like ' % / [ %' ESCAPE ‘ / ’(查询名字中带有 [ 的名字)
select * from TblStudent where name like '% / [%/]% ' ESCAPE ' / '
五、空值的处理
select * form TblStudent where age is null(查询所有年龄是null的同学信息)
select * form TbStudent where age is not null (查询不为null的)
select 2000 + null (注意:任何值和null计算得到的结果还是null)
//注:本文如果存在技术上的缺陷与问题,欢迎留言,后面会不断对本文进行补充。