用SQL进行单表查询

  单表查询是相对多表查询而言的,指从一个数据表中查询数据。
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。

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

时间: 2024-08-04 14:33:29

用SQL进行单表查询的相关文章

sql语句 连表查询-连表查询 sql语句问题

问题描述 连表查询 sql语句问题 有个商品表,和商品属性表,一个商品,在商品属性表里有好几条,我想连表查出,一条商品对应的两个属性,两个属性在一行显示,怎么查,属性表的数据是这样 1 商品id 规格 2 商品id 厂家 我想要的结果: 1 商品id 规格 厂家 大能们,帮帮忙 解决方案 查询库中有多少表的SQL语句单表查询树形结构的SQL语句命名查询的sql语句的问题 解决方案二: 你select出来的结果当成colum

求一条sql语句,单表查询的

问题描述 求一条sql语句,单表查询的 表结构 (姓名,课程,成绩) 现在求获的该表总成绩最高学员的名字 解决方案 mysql 数据库select sum(成绩) as t from table group by 姓名 order by t limit 0,1 oracle数据库 select * from (select sum(成绩) as t from table group by 姓名 order by t) WHERE ROWNUM<=1 sqlserver数据库 select top

python实现简易数据库(二) 单表查询和top N实现

上一篇中,介绍了我们的存储和索引建立过程,这篇将介绍SQL查询.单表查询和TOPN实现. 一.SQL解析 正规的sql解析是用语法分析器,但是我找了好久,只知道可以用YACC.BISON等,sqlite使用的lemon,捣整了一天没实现,就用了python的正则表达式. 1.删除无用的空格.跳格符.换行符等: 我们以分号';'作为一个sql语句的结束符,在输入分号之前,我们将输入的sql语句串接成一个string,在将整个sql语句的一些无用的字符删掉, 1 def rmNoUseChar(sq

6.单表查询

1.语法格式         select语句的功能就是查询数据,在SQL语句中功能最丰富,可单表查询.多表连接查询.子查询. SELECT  NAME, grade FROM student WHERE grade >80; 数据源student可以是表.视图等:select后列表用于确定选择哪些列(* 即所有列),where确定选择哪些行(无则选出所有行). select语句中可使用算术运算符(+.-.*./)形成算术表达式,用于数值型.日期型的数据列.变量.常量:运算符可在两列间进行运算.

sql语句多表查询中聚合函数的使用问题

问题描述 sql语句多表查询中聚合函数的使用问题 表A,B,C. A表中有title,code, b表中为code,media_code;(均不为主键,code会有重复) c表中有media_code,shrink (media_code为主键) 我的目的为取出A表中的title和c表中的shrink 联立:A.code→B.code, B.media_code→C.media_code 代码如下 select title,shrink from tableA,tableC where medi

用SQL进行多表查询

  所谓多表查询是相对单表而言的,指从多个数据表中查询数据,这里我们主要学习从两个数据表中如何查询数据的方法.4.3.1 无条件多表查询    无条件多表查询是将各表的记录以"笛卡尔"积的方式组合起来.    如scott.dept表共有4条记录,scott.emp表共有14条记录,其"笛卡尔"积将有4*14=56条记录.    在[命令编辑区]执行下列语句.    ――――――――――――――――――――――――――――――――――――――     select

SQL Server 交叉表查询 case_MsSql

代码如下所示: 表landundertake结构如下所示: 表appraiser结构如下所示: access代码: 复制代码 代码如下: TRANSFORM First(Landundertake.valuerId) AS valuerId之First SELECT Appraiser.quarterId, Landundertake.landCode FROM Landundertake INNER JOIN Appraiser ON (Landundertake .valuerId = Ap

oracle 全表单表查询慢,如何解决

问题描述 各位高手如题,表结构如图!~ 问题补充:那请问如果有大文本的内容的时候不用clob,那如何办?!tnt-scott 写道 解决方案 1.检索lob字段的时候,按需去取lob字段,如果你不用,则给它设成null,比如我要id='10'的lob字段,那么sql:select decode(id,'10',lob_col,null) from tb2.给lob字段建立单独的表空间,并设定把CACHE 设定成reads提高读取速度.代码:--创建表空间CREATE TABLESPACE MON

【sql查询与优化】1.单表查询

注:以下所有sql案例均取自"oracle查询优化改写技巧与案例"丛书. EMP表的详细: 1.查询表中所有的行与列 查询所有信息, SQL> select * from emp;      EMPNO ENAME                JOB                       MGR HIREDATE       SAL        COMM       DEPTNO ---------- -------------------- -------------