`
eminem
  • 浏览: 136741 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

SQL的资料

    博客分类:
  • SQL
阅读更多
1、select语句中使用top的一些小技巧

语句top,可以限制返回的记录数。
例如: select top 10 from table order by col

但是在使用的时候,有时候会遇到一些问题。
比如希望返回前三名的比分,但是第三名有并列的,使用select top 3 * from table order by col 的话就只能返回三条记录,在这样的情况下,就可以使用select top 3 with ties * from table order by col

还有,有的时候我不希望出现重复的记录,那么可以使用select distinct top 3 * from table order by col

使用TOP   n   [PERCENT]选项限制返回的数据行数,TOP   n说明返回n行,而TOP   n   PERCENT时,说明n是表示一百分数,指定返回的行数等于总行数的百分之几。
  例如:

      SELECT   TOP   2   *
  FROM   testtable
  SELECT   TOP   20   PERCENT   *
  FROM   testtable
上面的在DB2里不知道为什么没用?
select * from  PTY_PartyRole fetch first 5 rows only 我是这样的实现的(在DB2里可以,在MYsql里不可以)


2、rs.open sql,conn,1,1 的后两个 1,1 各代表什么?

RS.OPEN SQL,CONN,A,B

A: ADOPENFORWARDONLY(=0) 只读,且当前数据记录只能向下移动
   ADOPENSTATIC(=3) 只读,当前数据记录可自由移动
   ADOPENKEYSET(=1) 可读写,当前数据记录可自由移动
   ADOPENDYNAMIC(=2) 可读写,当前数据记录可自由移动,可看到新增记录

B: ADLOCKREADONLY(=1) 默认值,用来打开只读记录
   ADLOCKPESSIMISTIC(=2) 悲观锁定
   ADLOCKOPTIMISTIC(=3) 乐观锁定
   ADLOCKBATCHOPTIMISTIC(=4) 批次乐观锁定

//////引用资料

用SQL进行表单查询
        现把整理和积累的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 not 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 时不能使用   having ,但可以使用 where
字段运算查询:
                             select empno , ename , sal , mgr , sal + mgr from 数据表
                             利用算术运算仅仅适合多个数值型字段或字段与数字之间的运算
变换查询显示:
                             select empno 编号, ename 姓名, job 工作, sal 薪水, from 数据表


用SQL进行多表查询

无条件多表查询:
                                 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


用SQL进行嵌套查询

简单嵌套查询:
                              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的薪水的员工。
带in 的嵌套查询:
                     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 来查询
带 any 的嵌套查询:
                     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
带 some 的嵌套查询:
                     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
带 all 的嵌套查询:
                     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
带 exists 的嵌套查询:
                     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)


用SQL进行函数查询

ceil 函数:
                     select mgr,mgr/100,ceil(mgr/100) from scott.emp
                     ceil(n),取大于等于数值n的最小整数。
floor 函数:
                     select mgr, mgr/100,floor(mgr/100) from scott.emp
                     floor(n),取小于等于数值Nde 最大整数。
mod 函数:
                     select mgr, mod(mgr,1000), mod(mgr,100), mod(mgr,10) from scott.emp
                     mod(m,n),取m整除n后的余数。
power 函数:
                     select mgr,power(mgr,2),power(mgr,3) from scott.emp   
                     power(m,n),取m的n次方。
round 函数:
                     select mgr, round(mgr/100,2),round(mgr/1000,2) from scott.emp
                     round(m,n),四舍五入,保留n位。
sign函数:
                     select mgr,mgr-7800,sign(mgr-7800) from scott.emp
                     sign(n),n>0,取1;n=0,取0;n<0>取-1。
avg函数:
                     select avg(mgr) 平均薪水 from scott.emp
                     avg(字段名),求平均值。要求字段为数值型。
count函数:
                     select count(*) 记录总数 from scott.emp
                     count(字段名) 或 count(*),统计总数。
min 函数:
                     select min(sal) 最少薪水 from scott.emp
                     min(字段名),计算数值型字段最小数。
max函数:
                     select max(sal) 最高薪水 from scott.emp
                     max(字段名),计算数值型字段最大数。
sum 函数:
                  select sum(sal) 薪水总和 from scott.emp
                     sum(字段名),计算数值型字段总和。


用SQL录入数据

单行记录的录入:
                            Insert into 数据表(字段名1,字段名2)values(字段名1的值,字段名2的值)
注意:数值型字段,可以直接写值。字符型字段,要加单引号。日期型字段,要加上单引号。
多行记录的录入:
在数据的录入中,经常要将从数据表中查询到的数据稍做修改成批录入的情况,就是多行数据的录入。
Insert into 数据表 (字段名1,字段名2)(select (字段名1或运算,字段名2或运算) from 数据表 where 查询条件)
表间数据复制:
                            可以从一个数据表中选择需要的数据插入到全新的数据表中。
                     create table scott.test
                     as
                     (
                            select distinct empno,ename,hirdate
                            from scott.emp
                            where empno >= 7000
)


用SQL删除数据
删除记录:
                     delete from scott.test where empno >= 7500 and empno <= 8000
整表数据删除:
                     truncate table scott.test
truncate table 命令将快速删除数据表中的所有记录,但保留数据表的结构。这种快速删除与delete from数据表的删除全部数据表记录不一样,delete命令删除的数据将存储在系统回滚段中,需要的时候,数据可以回滚恢复,而truncate命令删除的数据是不可以恢复的。


用SQL更新数据
直接赋值更新:
                     update 数据表
                     set 字段名1 = 新的值,字段名2 = 新的值
                     where 条件
                     例:
                     update scott.emp
                     set empno = 8888,ename = ‘TOM’,hiredate = ’03-9 月-2002’
                     where empno = 7878

嵌套更新:
                     update 数据表
                     set 字段名1 = (select 字段列表 from 数据表 where 条件),字段名2 = (select 字段列表 from 数据表 where 条件)               ……
                     例:
                     update scott.emp
                     set sal =
                     (
                            select sal + 300 from scott.emp
                            where empno = 8888
)
                     where empno = 8888
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics