现在的位置: 首页 > 综合 > 正文

sql查询汇总1

2013年10月03日 ⁄ 综合 ⁄ 共 4653字 ⁄ 字号 评论关闭

这是我自己在学习sqlsever当中写的查询  菜鸟进来看看啊  总结的很详细的

资料来源   http://www.17xuexiba.com/index.php/archives/611

sql简单查询语句汇总一,是基于sql sever2005 和数据库scott的 。scott和查询的文件会提供下载,直接导入sql sever就行了

--计算列查询

select * from emp;
--*表示全部 的信息
select ename ,job from emp;
select ename job from emp;
--查寻字段时,字段与字段之间用逗号隔开,否则前一个字段名会变成后一个字段名
select sal,sal*12 as "年薪" from emp;
--计算列,这里 as可用可不用,"年薪"的双引号可以省略,但为了兼容性,不建议省略
select 8 from emp;
--这是可执行的,但不建议这样使用
select 8;
--这样也是行的

--between查询

--where 起一个过滤作用
select * from emp where sal>=1500 and sal<=3000;--选择工资在1500到3000之间(包括1500和3000)
--等价于
select *from emp where sal between 1500 and 3000; --between的用法

--选择工资大于3000或者 小于1500
select *from emp where sal<1500 or sal>3000;

等价于
select *from emp where not sal between 1500 and 3000;

 

--distinct查询

select deptno from emp;--输出14条记录 而不是五条
select 100from emp;--也是14条 而不是一条
select 5; --输出一条记录
select distinct deptno from emp;--过滤条重复的记录,只显示3条记录
select distinct comm from emp; --null作为一个记录,所以应该过滤掉
select distinct comm,deptno from emp; --两个字段合并起来过滤
select comm distinct ,deptno from emp;--发生错误,因为如果能查找出来,来个字段记录数量不一样,无法组成表

--group by查询

select * from emp;
select deptno from emp group by deptno; --group by +字段名
select deptno ,avg(sal)"平均工资" from emp group by deptno; --group 一般与聚合函数连用
select deptno ,avg(sal) "平均工资" ,min(sal) "最小工资" from emp group by deptno;

--下面是group by a,b,c ....的用法 先按a分组,如果a相同 ,在按b分组,如果b相同,则按c分组 ,一次类推
select deptno ,job ,avg(sal) "各部门不同职位的平均工资" from emp group by deptno,job;--多个字段连用时是以最后面的字段为小组,而前面的字段
--为大组的,在用聚合函数进行处理是 也是计算小组的.
select deptno ,job ,avg(sal) "各部门不同职位的平均工资" from emp group by deptno; --error 分组后只能出现分组后的整体信息,
--不能出现组内详细信息
select deptno ,job ,avg(sal) "各部门不同职位的平均工资" from emp group by deptno,job order by deptno;--与其他函数连用
select comm from emp group by comm;--null单独算一组
select deptno ,count(*) from emp group by deptno --count(*) 是针对分组后 每组的个数 而不是全部的
select deptno count(*) from emp group by deptno --这样是错误的

--in查询

--in查询孤立的值 查询某一个或者某几个
select * from emp where sal in(1500,3000,5000); --in后面括号里面就是该字段相应的值 ,只是几个值 而不是一个范围
等价于
select *from emp where sal=1500 or sal=3000 or sal=5000;

--不去这几个数
select* from emp where sal not in(1500,3000,5000);
等价于
select *from emp where sal!=1500 and sal!=3000 and sal!=5000;
等价于
select *from emp where sal<>1500 and sal<>3000 and sal<>5000;
--数据库里面不等于有两种表示方法 !=和<> 建议使用后面的

--null查询

select * from emp;
select * from emp where sal<> null;--错误 null值不能参与 <>运算
select * from emp where sal !=null;--错误 null值不能参与!= 和 =运算
select sal*12+comm from emp; --null不能参与算数运算,否则永远为空值
select * from emp where comm is null;--null值能参与null运算
select * from emp where comm is not null; --null 值能参与is not 运算

--order by查询

select * from emp;
select * from emp order by deptno;--默认按升序排列
select * from emp order by deptno desc; --desc按降序排列
select * from emp order by deptno,sal;--先按deptno的升序排列,如果相同 再按sal的升序排列 而并非组合的排序
select * from emp order by deptno desc,sal;--先按deptno降序排列,如果相同 在按sal升序排列,前面的排列顺序不影响后面的排列顺序
select * from emp order by deptno,sal desc;--先按deptno升序排列,如果相同 在按sal降序排列,后面的排列顺序不影响前面的排列顺序

--top查询

select * from emp;
select top 5 * from emp;--查询前5条记录 注意* 是在from前面
select top 15 percent * from emp;--是3条记录 而不是 2条

--选择工资在1500到3000内的前四个人
select top 4 * from emp --先选择
where sal between 1500 and 3000 --再过滤
order by sal desc; --decs是按降序排列 默认的升序-- 在排序
--在oracle 里面用的不是top 而是 rownum

--聚合查询

--单行函数 是指 每一行记录返回一个值
select lower(ename) from emp;-- 将ename字段字母变成小写的 这是单行函数
--多行行数 聚合函数就是多行函数 是指 多行记录返回一个值
select min(sal) as "最低工资" from emp; --返回 sal字段最低值
select max(sal) as "最高工资" from emp; --返回sal字段中最大值
select avg(sal) as "平均工资" from emp; --返回sal字段中平均值

--下面是count的用法
select count(*) as "总记录项数" from emp; --返回一行值 而不是十四行 返回的是记录的项数
select count(deptno) from emp; --返回的也是十四行 ,重复的也算有效值
select count(distinct deptno) from emp;--返回的是3行
select count(comm)from emp; --返回四行 null值不算有效值
select max(sal),min(sal),count(*) from emp;--返回一行值 是有效的
select max(sal),lower(ename) from emp;--错误 因为单行行数和聚合函数不可以合用

--模糊查询

--模糊查询格式 select字段的集合 from 表名 where 某个字段的名字 like 匹配的条件
--匹配的条件通常含有通配符
select * from emp where ename like '%A%';--前后都有%说明ename字段中凡是有A的都输出
select * from emp where ename like '%R';--前面有% 说明凡是ename字段中末尾字符为R的输出
select * from emp where ename like 'A%';--后面有% 说明首字母为A 的都输出
select * from emp where ename like '_A%';--_说明 ename字段中第二个字母为A的输出
select * from emp where ename like '[a-f]%';--首字母为 a到f中任意字母的输出
select * from emp where ename like '_[a-f]%';--第二个字母为 a到f中任意字母的输出
select * from emp where ename like '__[a-f]%';--第三个字母为 a到f中任意字母的输出
select * from emp where ename like '[a,f]%';--第一个字母为 a或f的输出
select * from emp where ename like '~[a-f]%';--待定
select * from emp where ename like '%\%%' escape '\';--这里escape 后面定义的字符当做转意字符 这里将字段中含有转意字符后面字母的输出

--having查询

select * from emp;
--having的用法
select deptno ,avg(sal) 平均工资 from emp group by deptno having avg(sal)>2000 ;--having 一般是和分组一起用的,是对分组后的在一次筛选
--having和where的区别
select deptno from emp where deptno>20; --where 是对原始数据的过滤
select deptno from emp group by deptno having deptno>20;--having是多分组后的过滤
select deptno 部门 from emp where 部门>20; --错误 --where 和 having 都不能用字段的别名

--一下查询语句中where和having语句不能颠倒 where在having前面
select deptno ,avg(sal) 平均工资,count(*) "部门人数",max(sal) 最高工资 from emp
where sal>1500 --对整体工资进行过滤
group by deptno --对数据进行分组
having avg(sal)>2000 --对分组后的数据进行 过滤
order by avg(sal) desc --对过滤后的数据进行排序

--having语句不显示组内详细信息

来源  http://www.17xuexiba.com/index.php/archives/611

 点此进入sql查询2内连接

文件下载地址:115下载   华为网盘下载

 

 

sq1

抱歉!评论已关闭.