TA的每日心情 | 开心 2015-10-14 21:28 |
---|
签到天数: 31 天 连续签到: 1 天 [LV.5]常住居民I
|
东陆风华帐号全站通用,包括论坛、商城、网络家园等站点,登陆查看更精彩内容
您需要 登录 才可以下载或查看,没有帐号?注册东陆风华通行证
x
现把整理和积累的SQL常用语句公开给大家,以方便大家在日常工作中使用.同时也希望各位同僚,继续提出宝贵建议,作出补充.显示数据表的结构:
desc 数据表名
查询所有记录:
select * from 数据表
查询所有记录的某些字段:
select 字段名1,字段名2 from 数据表
select name,age from 数据表
查询某些字段的不同记录:
select distinct job from 数据表
在显示时去除相同的记录
单条件查询:
select * from数据表 where sal <= 2500
select * from数据表 where job != MANAGER
select * from数据表 where job ^= MANAGER
select * from数据表 where job <> MANAGER
select * from 数据表 where sal ^= 1000
select * from 数据表 where sal in(2000,1000,3000) / not in
select * from 数据表 where job in(MANAGER,CLERK)
select * from 数据表 where sal between 2000 and 3000 / not between
select * from 数据表 where job between MANAGER and CLERK
select * from 数据表 where job like M% / not like
select * from 数据表 where job like M_
like 和 not like 适合字符型字段的查询,%代表任意长度的字符串,_代表一个任意的字符Like m%代表 m 开头的任意长度的字符串,like m__代表 m 开头的长度为3 的字符串
select * from 数据表 where sal is null / not null
select * from 数据表 where job is null
组合条件的查询:
select * from 数据表 where job >= CLERK and sal <= 2000
select * from 数据表 where job >= CLERK or sal <= 2000
逻辑非组合查询结果
not job = CLERK 等价于 job <> CLERK
select * from 数据表 where job <= CLERK order by job asc, sal desc
select * from 数据表 order by job asc, sal desc
order by 可以指定查询结果如何排序,形式为字段名排序关键词;asc 代表升序排列,desc 代表降序排列,多个排序字段之间通过逗号分割若有 where 查询条件, order by 要放在 where 语句后面
分组查询:
将查询结果按照字段分组
select empno, ename, job, sal from scott.emp group by job, empno, ename,sal having sall <= 2000
select empne, ename, job, sal from scott.emp where sal <=2000 group by job, empno, ename, sal
注:group by 后的字段必须与前面select 后的字段相对应
where 检查每条记录是否符合条件,having 是检查分组后的各组是否满足条件having 语句只能配合 group by 语句使用,没有 group by 时不能使用,但可以使用 where
字段运算查询:
select empno , ename , sal , mgr , sal + mgr from 数据表
利用算术运算仅仅适合多个数值型字段或字段与数字之间的运算
变换查询显示:
无条件多表查询:
select emp.empno, emp.ename, emp.deptno, dept.dname, dept.loc
from scott.emp, scott.dept
等值多表查询:
select emp.empno, emp.ename, em.deptno, dept.dname, dept.loc
from scott.emp, scott.dept
where scott.emp.deptno = scott.dept.deptno
等值多表查询将按照等值的条件查询多个数据表中关联的数据要求关联的多个数据表的某些字段具有相同的属性,即具有相同的数据类型,宽度和取值范围
非等值多表查询:
select emp.empno, emp.ename, em.deptno, dept.dname, dept.loc
from scott.emp, scott.dept
where scott.emp.deptno != scott.dept.deptno and scott.emp.deptno = 10
简单嵌套查询:
select emp.empno, emp.ename, emp.job, emp.sal
from scott.emp
where sal >= (select sal from scott.emp where ename = WARD)
在这段代码中,子查询select sal from scott.emp where ename = WARD的含义是从emp数据表中查询姓名为WARD的员工的薪水,父查询的含义是要找出emp数据表中薪水大于等于WARD的薪水的员工
select emp.empno, emp.ename, emp.job, emp.sal
from scott.emp
where sal in (select sal from scott.emp where ename = WARD)
查询薪水和WARD相等的员工,也可以使用 not in 来查询
select emp.empno, emp.ename, emp.job, emp.sal
from scott.emp
where sal > any(select sal from scott.emp where job = MANAGER)
等价于:select sal from scott.emp where job = MANAGER
查询结果为:1000,2500
select emp.empno, emp.ename, emp.job, emp.sal
from scott.emp
where sal > 1000 or sal > 2500
select emp.empno, emp.ename, emp.job, emp.sal
from scott.emp
where sal = some (select sal from scott.emp where job = MANAGER)
等价于:select sal from scott.emp where job = MANAGER
查询结果为:1000,2500
select emp.empno, emp.ename, emp.job, emp.sal
from scott.emp
where sal = 1000 or sal = 2500
select emp.empno, emp.ename, emp.job, emp.sal
from scott.emp
where sal > all (select sal from scott.emp where job = MANAGER)
等价于:select sal from scott.emp where job = MANAGER
查询结果为:1000,2500
select emp.empno, emp.ename, emp.job, emp.sal
from scott.emp
where sal > 1000 and sal > 2500
select emp.empno, emp.ename, emp.job, emp.sal
from scott.emp, scott.dept
where exists (select * from scott.emp where scott.emp.deptno = scott.dept.deptno)
等价于:select sal from scott.emp where job = MANAGER
查询结果为:1000,2500
select emp.empno, emp.ename, emp.job, emp.sal
from scott.emp
where sal = 1000 and sal = 2500
并操作的嵌套查询:
并操作就是集合中并集的概念属于集合A 或集合B的元素总和就是并集
(select deptno from scott.emp)
union
(select deptno from scott.dept)
交操作的嵌套查询:
交操作就是集合中交集的概念属于集合A 且属于集合B的元素总和是并集
(select deptno from scott.emp)
intersect
(select deptno from scottdept)
差操作的嵌套查询:
差操作就是差集的概念属于集合A 且不属于集合B的元素总和是并集
(select deptno from scott.emp)
minus
(select deptno from scottdept) |
|