东陆风华BBS

 找回密码
 注册东陆风华通行证
东陆风华论坛18周年庆(2005.3.28-2023.3.28)东陆风华-云南大学各专业历年考研复试信息汇总帖送人玫瑰手留余香-云南大学历年考研复试信息汇总
东陆风华-云南大学各学院考研QQ群号码
申请东陆风华实名认证免费领取云大考研考博真题如何申请云南大学考研论坛VIP会员?VIP特权?
东陆风华-云南大学各省校友&云南省各地州校友QQ群云南大学呈贡校区图片 &宿舍图片&图片云大 东陆风华送人玫瑰手留余香-云南大学2021年考研复试信息分享
返回列表 发新帖
查看: 1227|回复: 0

SQL语法整理(一)

[复制链接]
  • TA的每日心情
    开心
    2015-10-14 21:28
  • 签到天数: 31 天

    连续签到: 1 天

    [LV.5]常住居民I

    发表于 2008-9-10 14:07:10 | 显示全部楼层 |阅读模式

    东陆风华帐号全站通用,包括论坛、商城、网络家园等站点,登陆查看更精彩内容

    您需要 登录 才可以下载或查看,没有帐号?注册东陆风华通行证

    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)
    东陆风华APP客户端 http://bbs.ynutx.net/appbyme_app-download.html

    本版积分规则

    关闭

    站长推荐上一条 /2 下一条

    QQ|小黑屋|手机版|Archiver|东陆风华,凝聚云大人的力量 ( 滇ICP备07500061号-1 )

    GMT+8, 2024-5-6 13:15 , Processed in 0.093750 second(s), 19 queries , Gzip On, Redis On.

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表