SQL-select
来源:互联网 发布:期货行情数据接口 编辑:程序博客网 时间:2024/06/06 02:34
1.基本查询
//查询所有列select * from emp;//依靠列名查询select empno,ename,job,mgr,hiredate,sal,comm,deptno from emp;//查询中带有表达式select empno,ename,sal,sal*12 from emp;select empno,ename,sal,sal*12,comm,sal*12+comm from emp;//nvl(a,b)sql中存在null 若a为null则a=b,否则为aselect empno,ename,sal,sal*12,comm,sal*12+nvl(comm,0)from emp;//distinct去除重复记录,作用于后面所有的列select distinct deptno from emp;select distinct deptno,job from emp
2.连接符||
select ename||'的薪水是:'|| sal from emp;select concat('hello','world') from dual;
3.sql中的字符串
sql中字符串大小写敏感
日期格式敏感
//查询日期格式select * from v$nls_parameters;alter session/system set NLS_DATE_FORMAT='yyyy-mm-dd'// 会话中/系统中 修改
4.模糊查询like
% _
//模糊查询,将姓名中带有_的员工找出来 _是sql中的特殊字符,需要转议,使用escape 关键字将\进行转议select * from emp where ename like '%\_%' escape '\' ;
5.排序
- asc 默认 升序
- desc 降序
- order by 列/表达式/别名/序号(select语句中列的值1,2,3,4…..)
多个列排序
- order by 作用于后边所有的列
- 先按照第一个列排序,若第一个列相同,再按照第二个列排序
- desc只作用于最近的列
6.多表查询
(笛卡儿积)
连接条件
- 2张表:1个条件
- 3张表:2个条件
- n张表:n-1个条件
多表查询
- 等值连接
- 不等值连接
- 外连接
- 自连接
--等值连接--查询员工信息:员工号,姓名,月薪,部门名称select * from emp;select * from dept;select e.empno,e.ename,e.sal,d.dnamefrom emp e,dept dwhere e.deptno = d.deptno;--不等值链接--查询员工信息:员工号,姓名,月薪,工资级别select * from salgrade;--工资等级表select e.empno,e.ename,e.sal,s.gradefrom emp e,salgrade swhere e.sal between s.losal and s.hisal;--外连接--按照部门统计员工人数:部门号,部门名称,人数select d.deptno 部门号,d.dname 部门名称,count(e.deptno) 人数 from emp e,dept dwhere e.deptno=d.deptnogroup by d.deptno,d.dname; --左外:当where e.deptno=d.deptno不成立的时候,等号左边的表依然被包含在内 --写法:where e.deptno=d.deptno(+) --右外:当where e.deptno=d.deptno不成立的时候,等号右边的表依然被包含在内 --写法:where e.deptno(+)=d.deptno select d.deptno 部门号,d.dname 部门名称,count(e.deptno) 人数 from emp e,dept d where e.deptno(+)=d.deptno group by d.deptno,d.dname;--自连接:通过表的别名,将同一张表视为多张表--查询员工信息: 员工的姓名 老板的姓名select e.ename 员工姓名,b.ename 老板姓名from emp e,emp bwhere e.mgr=b.empnoorder by b.ename;--自连接不适合操作大表--层次查询-树状图结构select level,empno,ename,mgrfrom empconnect by prior empno=mgrstart with mgr is nullorder by level;
7.子查询
注意问题:
- 括号
- 合理的书写风格
- 可以在住查询的where select having from 后面放置子查询
- 不可以在group by 后面放置子查询
- 强调from 后面的子查询
- 主查询和子查询可以不是同一张表,只要子查询返回的结果主查询可以使用即可
- 一般不在子查询中排序,但在TOP-N分析问题中,必须对子查询排序
- 一般先执行子查询,再执行主查询,但相关子查询例外
- 单行子查询只能使用单行操作符,多行子查询只能使用多行操作符
- 子查询中的null
--3.可以在主查询的where select having from后面放置子查询 select empno,ename,sal,(select job from emp where empno=7839) 第四列 from emp; --向主查询中的having子句返回结果 select deptno,min(sal) from emp group by deptno having min(sal)> ( select min(sal) from emp where deptno=10 );--5.强调from后面的子查询 --查询员工信息:员工号,姓名,月薪 select * from ( select empno,ename,sal from emp ); --查询员工信息:员工号,姓名,月薪,年薪 select * from ( select empno,ename,sal,sal*12 annlsal from emp );--6.主查询和子查询可以不是同一张表;只要子查询返回的结果主查询可以使用即可 --查询名称是SALES部门的员工信息 select * from emp where deptno=( select deptno from dept where dname='SALES');--多行子查询中的null--查询不是老板的员工信息(不是老板即员工号不在mar列的员工)select * from emp where empno not in (select mgr from emp);--查询是老板的员工信息select * from empwhere empno in (select mgr from emp);--查询不是老板的员工信息(不是老板即员工号不在mar列的员工)select * from empwhere empno not in ( select mgr from emp where mgr is not null);
单行子查询
单行操作符
- =
- >
- >=
- <
- <=
- <>
多行子查询
多行操作符
- IN 等于列表中的任何一个
- ANY 和子查询返回的任意一个值比较
- ALL和子查询返回的所有值比较
--单行子查询select ename,job,salfrom empwhere job = (select job from emp where empno =7566) and sal > (select sal from emp where empno=7782);--在子查询中使用组函数select ename,job,salfrom empwhere sal=(select min(sal) from emp);--多表查询 查询名称是SALES部门的员工信息 select e.* from emp e, dept d where e.deptno=d.deptno and d.dname='SALES'; --in 在集合中 --查询部门名称是SALES和ACCOUNTING的员工 select * from emp where deptno in ( select deptno from dept where dname='SALES' or dname='ACCOUNTING' ); select e.* from emp e,dept d where e.deptno=d.deptno and(d.dname='SALES' OR d.dname='ACCOUNTING'); --any 和集合中的任意一个值比较 --查询工资比30号部门任意一个员工高的员工信息 select * from emp where sal>any( select sal from emp where deptno=30 ); --all: 和集合中的所有值比较 --查询工资比30号部门所有员工高的员工信息 select * from emp where sal>all( select sal from emp where deptno=30 );
0 0
- SQL Select
- SQL-SELECT
- select sql
- SQL select
- SQL SELECT
- SQL-select
- SQL SELECT
- SQL SELECT
- SQL Select
- Select Into & Insert Select --SQL
- SQL SELECT 和 SELECT * 语句
- SQL SELECT 语句
- SQL SELECT语法
- SQL Select Into
- SQL SELECT 语句
- T-SQL SELECT 查询
- oracel SQL select 1
- Select in SQL
- [POJ3352]Road Construction 做题笔记
- OSI七层模型详解
- 利用牛顿法接非线性方程组的Matlab程序实例
- 当数据不够一屏时 实现collectionView的滑动
- matlab随机数
- SQL-select
- [疯狂Java]UDP:接收发送数据报、获取接收到的数据报的相关信息
- 目标跟踪 最简单的目标跟踪(模版匹配)
- linux 下硬链接和软连接
- Web攻防系列教程之跨站脚本攻击和防范技巧详解
- HttpClient 4.5版本设置连接超时时间
- js中的arguments对象的使用
- 如何在ubuntu下桌面登录到BBB板
- Linux date命令