用户登陆:
设为首页 | 加入收藏 | 免费邮箱
建站学院: 建站心得 | 搜索优化 | 建站点子 | 网站推广 | 网站推荐 | 企业新闻 | 人物新闻 | 网媒内幕 | 域名相关 | 热门专题 | 本站作品
设计学院: 网页设计 | 平面设计 | 网络编程 | 多媒体类 | 数据库类 | 服务器类 | 操作系统 | 联盟新闻 | 联盟介绍 | 联盟点评 | 网赚技巧
模板下载: 旅游度假 | 饮食食品 | 公司展示 | 学校教育 | 文化艺术 | 金融财经 | 儿童卡通 | 体育运动 | 服饰品牌 | 爱情交友 | 游戏娱乐
您现在的位置: 星星雨设计站 >> 设计学院 >> 数据库类 >> Oracle >> 教程正文
用SQL进行单表查询
作者:佚名    教程来源:不详    点击数:    更新时间:2007-11-19

  单表查询是相对多表查询而言的,指从一个数据表中查询数据。
4.2.1 查询所有的记录
    在【命令编辑区】执行输入“select * from scott.emp”,然后单击【执行】按钮,出现如图4.3所示的emp数据表所有记录。
    【参见光盘文件】:\第4章\4.2\421.sql。

    select * from 数据表,这里的“*”代表数据表中所有的字段。
4.2.2 查询所有记录的某些字段
    在【命令编辑区】输入“select empno,ename,job from scott.emp”,然后单击【执行】按钮,将显示emp数据表的empno、ename和job字段,如图4.4所示。
    【参见光盘文件】:\第4章\4.2\422.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。

    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。

    (2)在【命令编辑区】输入“select empno,ename,sal from scott.emp where sal<=2500”,然后单击【执行】按钮,出现如图4.7所示的数字型字段条件查询的结果,查询的是满足sal小于等于2500的记录。
    【参见光盘文件】:\第4章\4.2\424-2.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。

    (2)在【命令编辑区】输入“select empno,ename,job from scott.emp where job>=’CLERK’ or sal<=2000”,然后单击【执行】按钮,出现如图4.9所示的逻辑或组合查询的结果。
    【参见光盘文件】:\第4章\4.2\425-2.sql。

    (3)在【命令编辑区】输入“select empno,ename,job from scott.emp where not job=’CLERK’”,然后单击【执行】按钮,出现如图4.10所示的逻辑非组合查询的结果。
    【参见光盘文件】:\第4章\4.2\425-3.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。

    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。

    (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。

    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。

    利用算术运算符仅仅适合多个数值型字段或字段与数字之间的运算。
4.2.9 变换查询显示
    在【命令编辑区】输入“select empno 编号,ename 姓名,job 工作,sal 薪水 from scott.emp”,然后单击【执行】按钮,出现如图4.15所示的结果,可以将默认的字段名以设定的名称显示。
    【参见光盘文件】:\第4章\4.2\429.sql。

    以上我们学习了对单个数据表的查询语句。将上面这些基本的实例经过组合,就可以完成基本的日常数据查询任务,接下来进一步学习多表查询。

发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口教程录入:冰河    责任编辑:冰河 
  • 上一篇教程:

  • 下一篇教程:
  •   网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
    最新热门
    最新推荐
    | 设为首页 | 加入收藏 | 联系站长 | 友情链接 | 版权申明