网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > 数据库 > Oracle教程
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
数据库:数据库教程,数据库技巧,Oracle教程,MySQL教程,Sybase教程,Access教程,DB2教程,数据库安全,数据库文摘
本月文章推荐
.层次树添加节点、删除节点.
.red hat liunx的几点小技巧.
.生僻的SQLPLUS命令.
.DOS 的使用者投向 Linux :介绍 02.
.Oracle数据库启动监听器不启动或.
.ORACLE中几种的数据类型的用法以.
.CSDN Oracle电子杂志创刊号发布.
.创建镜像备用数据库.
.Oracle诊断案例1 -SGA与Swap.
.如何利用java过程执行操作系统命.
.Oracle兔死狗烹?(zt).
.怎样快速查出Oracle 数据库中的锁.
.漫谈镜像(备份)网站的几种有效方.
.浅谈Oracle 10G 闪回恢.
.ORACLE常用傻瓜問題1000問(之三).
.Oracle9i初始化参数中文说明10.
.Oracle性能究极优化.
.Oracle 10g RMAN的备份测试.
.在Oracle8i的SQL*Plus中如何利用.
.Oracle弱智900问一.

用SQL进行单表查询

发表日期:2008-2-9


  单表查询是相对多表查询而言的,指从一个数据表中查询数据。
4.2.1 查询所有的记录
    在【命令编辑区】执行输入“select * from scott.emp”,然后单击【执行】按钮,出现如图4.3所示的emp数据表所有记录。
    【参见光盘文件】:\第4章\4.2\421.sql。
用SQL进行单表查询
    select * from 数据表,这里的“*”代表数据表中所有的字段。
4.2.2 查询所有记录的某些字段
    在【命令编辑区】输入“select empno,ename,job from scott.emp”,然后单击【执行】按钮,将显示emp数据表的empno、ename和job字段,如图4.4所示。
    【参见光盘文件】:\第4章\4.2\422.sql。
用SQL进行单表查询
    select 字段名1, 字段名2,…… from 数据表,将显示某些特定的字段,注重这里的字段名之间的逗号是英文状态下的逗号。
4.2.3 查询某些字段不同记录
    在图4.4所示的job字段中,可以发现有相同的数据,为了查询有多少种不同的job,在【命令编辑区】输入“select distinct job from scott.emp”,然后单击【执行】按钮,出现如图4.5所示的结果。
    【参见光盘文件】:\第4章\4.2\423.sql。
用SQL进行单表查询
    select distinct 字段名 from 数据表,这里的“distinct”保留字指在显示时去除相同的记录,与之对应的是“all”将保留相同的记录,默认为“all”。
4.2.4 单条件的查询
    (1)在【命令编辑区】输入“select empno,ename,job from scott.emp where job=’MANAGER’”,然后单击【执行】按钮,出现如图4.6所示的字符型字段条件查询的结果,查询的是job为MANAGER的记录。
    【参见光盘文件】:\第4章\4.2\424-1.sql。
用SQL进行单表查询
    (2)在【命令编辑区】输入“select empno,ename,sal from scott.emp where sal<=2500”,然后单击【执行】按钮,出现如图4.7所示的数字型字段条件查询的结果,查询的是满足sal小于等于2500的记录。
    【参见光盘文件】:\第4章\4.2\424-2.sql。
用SQL进行单表查询
    where可以指定查询条件,假如是指定字符型字段查询条件,形式为字段名 运算符 '字符串';假如是指定数字型字段查询条件,形式为字段名 运算符 '字符串'。 单条件查询使用的比较运算符如表4.1所示。
    【参见光盘文件】:\第4章\4.2\table41.sql。
表4.1 比较运算符 名称 实例 =(等于) select * from scott.emp where job=’MANAGER’; select * from scott.emp where sal=1100; != (不等于) select * from scott.emp where job!=’MANAGER’; select * from scott.emp where sal!=1100; ^=(不等于) select * from scott.emp where job^=’MANAGER’; select * from scott.emp where sal^=1100; <>(不等于) select * from scott.emp where job<>’MANAGER’; select * from scott.emp where sal<>1100; <(小于) select * from scott.emp where sal<2000; select * from scott.emp where job<’MANAGER’; >(大于) select * from scott.emp where sal>2000; select * from scott.emp where job>’MANAGER’; <=(小于等于) select * from scott.emp where sal<=2000; select * from scott.emp where job<=’MANAGER’; >=(大于等于) select * from scott.emp where sal>=2000; select * from scott.emp where job>=’MANAGER’; in(列表) select * from scott.emp where sal in (2000,1000,3000); select * from scott.emp where job in (’MANAGER’,’CLERK’); not in(不在列表) select * from scott.emp where sal not in (2000,1000,3000); select * from scott.emp where job not in (’MANAGER’,’CLERK’); between(介于之间) select * from scott.emp where sal between 2000 and 3000; select * from scott.emp where job between ’MANAGER’ and ’CLERK’; not between (不介于之间) select * from scott.emp where sal not between 2000 and 3000; select * from scott.emp where job not between ’MANAGER’ and ’CLERK’; like(模式匹配) select * from scott.emp where job like ’M%’; select * from scott.emp where job like ’M__’; not like (模式不匹配) select * from scott.emp where job not like ’M%’; select * from scott.emp where job not like ’M__’; Is null (是否为空) select * from scott.emp where sal is null; select * from scott.emp where job is null; is not null(是否为空) select * from scott.emp where sal is not null; select * from scott.emp where job is not null;
    like和not like适合字符型字段的查询,%代表任意长度的字符串,_下划线代表一个任意的字符。like ‘m%’ 代表m开头的任意长度的字符串,like ‘m__’ 代表m开头的长度为3的字符串。
4.2.5 组合条件的查询
    (1)在【命令编辑区】输入“select empno,ename,job from scott.emp where job>=’CLERK’ and sal<=2000”,然后单击【执行】按钮,出现如图4.8所示的逻辑与组合查询的结果。
    【参见光盘文件】:\第4章\4.2\425-1.sql。
用SQL进行单表查询
    (2)在【命令编辑区】输入“select empno,ename,job from scott.emp where job>=’CLERK’ or sal<=2000”,然后单击【执行】按钮,出现如图4.9所示的逻辑或组合查询的结果。
    【参见光盘文件】:\第4章\4.2\425-2.sql。
用SQL进行单表查询
    (3)在【命令编辑区】输入“select empno,ename,job from scott.emp where not job=’CLERK’”,然后单击【执行】按钮,出现如图4.10所示的逻辑非组合查询的结果。
    【参见光盘文件】:\第4章\4.2\425-3.sql。
用SQL进行单表查询 
    “not job=’CLERK’”等价于“job<>’CLERK’”。
    组合条件中使用的逻辑比较符如表4.2所示。
    【参见光盘文件】:\第4章\4.2\table42.sql。
表4.2 逻辑比较符 名称 实例 and(与) select * from scott.emp where job=’MANAGER’ and sal<>2000; or (或) select * from scott.emp where job!=’MANAGER’ or sal<>2000; not(非) select * from scott.emp where not job>=’MANAGER’; 4.2.6 排序查询
    在【命令编辑区】输入“select empno,ename,job from scott.emp where job<=’CLERK’ order by job asc,sal desc”,然后单击【执行】按钮,出现如图4.11所示的排序查询的结果。
    【参见光盘文件】:\第4章\4.2\426.sql。
用SQL进行单表查询
    order by 可以指定查询结果如何排序,形式为字段名 排序要害词;asc代表升序排列,desc代表降序排列,多个排序字段之间通过逗号分割。若有where查询条件,order by要放在where语句后面。
4.2.7 分组查询
    分组查询是指将查询结果按照字段分组。
    (1)在【命令编辑区】输入“select empno,ename,job,sal from scott.emp group by job,empno,ename,sal having sal<=2000”,然后单击【执行】按钮,出现如图4.12所示的分组查询的结果。
    【参见光盘文件】:\第4章\4.2\427-1.sql。
用SQL进行单表查询
    (2)在【命令编辑区】输入“select empno,ename,job,sal from scott.emp where sal<=2000 group by job,empno,ename,sal”,然后单击【执行】按钮,出现如图4.13所示的分组查询的结果。
    【参见光盘文件】:\第4章\4.2\427-2.sql。
用SQL进行单表查询
    where检查每条记录是否符合条件,having是检查分组后的各组是否满足条件。having语句只能配合group by语句使用,没有group by时不能使用having,但可以使用where。
4.2.8 字段运算查询
    可以利用几种基本的算术运算符来查询数据。
    常见的+(加)、-(减)、*(乘)、/(除)4种算术运算都可以用来查询数据。
    在【命令编辑区】输入“select empno,ename,sal,mgr,sal+mgr from scott.emp”,然后单击【执行】按钮,出现如图4.14所示的结果。

    【参见光盘文件】:\第4章\4.2\428.sql。
用SQL进行单表查询
    利用算术运算符仅仅适合多个数值型字段或字段与数字之间的运算。
4.2.9 变换查询显示
    在【命令编辑区】输入“select empno 编号,ename 姓名,job 工作,sal 薪水 from scott.emp”,然后单击【执行】按钮,出现如图4.15所示的结果,可以将默认的字段名以设定的名称显示。
    【参见光盘文件】:\第4章\4.2\429.sql。
用SQL进行单表查询
    以上我们学习了对单个数据表的查询语句。将上面这些基本的实例经过组合,就可以完成基本的日常数据查询任务,接下来进一步学习多表查询。
上一篇:SQL概述 人气:569
下一篇:用SQL进行多表查询 人气:1153
浏览全部Oracle教程的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐