用SQL进行单表查询

2016-02-19 17:36 4 1 收藏

今天图老师小编给大家展示的是用SQL进行单表查询,精心挑选的内容希望大家多多支持、多多分享,喜欢就赶紧get哦!

【 tulaoshi.com - 编程语言 】

  单表查询是相对多表查询而言的,指从一个数据表中查询数据。

  4.2.1 查询所有的记录

  在执行输入select * from scott.emp,然后单击按钮,出现如图4.3所示的emp数据表所有记录。

  :第4章4.2421.sql。

  select * from 数据表,这里的*代表数据表中所有的字段。

  4.2.2 查询所有记录的某些字段

  在输入select empno,ename,job from scott.emp,然后单击按钮,将显示emp数据表的empno、ename和job字段,如图4.4所示。

  :第4章4.2422.sql。

  select 字段名1, 字段名2, from 数据表,将显示某些特定的字段,注意这里的字段名之间的逗号是英文状态下的逗号。

  4.2.3 查询某些字段不同记录

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

  在图4.4所示的job字段中,可以发现有相同的数据,为了查询有多少种不同的job,在输入select distinct job from scott.emp,然后单击按钮,出现如图4.5所示的结果。

  :第4章4.2423.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.2424-1.sql。

  (2)在输入select empno,ename,sal from scott.emp where sal=2500,然后单击按钮,出现如图4.7所示的数字型字段条件查询的结果,查询的是满足sal小于等于2500的记录。

  :第4章4.2424-2.sql。

  where可以指定查询条件,如果是指定字符型字段查询条件,形式为字段名 运算符 '字符串';如果是指定数字型字段查询条件,形式为字段名 运算符 '字符串'。 单条件查询使用的比较运算符如表4.1所示。

  :第4章4.2table41.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 sal1100;(小于)select * from scott.emp where sal2000;select * from scott.emp where job’MANAGER’;(大于)select * from scott.emp where sal2000;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.2425-1.sql。

  (2)在输入select empno,ename,job from scott.emp where job=’CLERK’ or sal=2000,然后单击按钮,出现如图4.9所示的逻辑或组合查询的结果。

  :第4章4.2425-2.sql。

  (3)在输入select empno,ename,job from scott.emp where not job=’CLERK’,然后单击按钮,出现如图4.10所示的逻辑非组合查询的结果。

  :第4章4.2425-3.sql。

 

  not job=’CLERK’等价于job’CLERK’。

  组合条件中使用的逻辑比较符如表4.2所示。

  :第4章4.2table42.sql。

  表4.2 逻辑比较符

名称实例and(与)select * from scott.emp where job=’MANAGER’ and sal2000;or (或)select * from scott.emp where job!=’MANAGER’ or sal2000;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.2426.sql。

  order by 可以指定查询结果如何排序,形式为字段名 排序关键词;asc代表升序排列,desc代表降序排列,多个排序字段之间通过逗号分割。若有where查询条件,order by要放在where语句后面。

  4.2.7 分组查询

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

  分组查询是指将查询结果按照字段分组。

  (1)在输入select empno,ename,job,sal from scott.emp group by job,empno,ename,sal having sal=2000,然后单击按钮,出现如图4.12所示的分组查询的结果。

  :第4章4.2427-1.sql。

  (2)在输入select empno,ename,job,sal from scott.emp where sal=2000 group by job,empno,ename,sal,然后单击按钮,出现如图4.13所示的分组查询的结果。

  :第4章4.2427-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.2428.sql。

  利用算术运算符仅仅适合多个数值型字段或字段与数字之间的运算。

  4.2.9 变换查询显示

  在输入select empno 编号,ename 姓名,job 工作,sal 薪水 from scott.emp,然后单击按钮,出现如图4.15所示的结果,可以将默认的字段名以设定的名称显示。

  :第4章4.2429.sql。

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

来源:https://www.tulaoshi.com/n/20160219/1615631.html

延伸阅读
标签: SQLServer
  连接查询 通过连接运算符可以实现多个表查询。连接是关系数据库模型的主要特点,也是它区别于其它类型数据库管理系统的一个标志。 在关系数据库管理系统中,表建立时各数据之间的关系不必确定,常把一个实体的所有信息存放在一个表中。当检索数据时,通过连接操作查询出存放在多个表中的不同实体的信息。连接操作给用户带来很大的灵...
标签: SQLServer
  联合查询 UNION运算符可以将两个或两个以上上SELECT语句的查询结果集合合并成一个结果集合显示,即执行联合查询。UNION的语法格式为: select_statement UNION [ALL] selectstatement [UNION [ALL] selectstatement][…n] 其中selectstatement为待联合的SELECT查询语句。 ALL选项表示将所有行合并到结果集合中。不指定该项...
SQL语言查询基础:连接查询  通过连接运算符可以实现多个表查询。连接是关系数据库模型的主要特点,也是它区别于其它类型数据库管理系统的一个标志。  在关系数据库管理系统中,表建立时各数据之间的关系不必确定,常把一个实体的所有信息存放在一个表中。当检索数据时,通过连接操作查询出存放在多个表中的不同实体的信息。连接操...
连接可以在Select 语句的FROM子句或Where子句中建立,似是而非在FROM子句中指出连接时有助于将连接操作与Where子句中的搜索条件区分开来。所以,在Transact-SQL中推荐使用这种方法。 SQL-92标准所定义的FROM子句的连接语法格式为: FROM join_table join_type join_table [ON (join_condition)] ...
新建两张表: 表1:student  截图如下: 表2:course  截图如下: (此时这样建表只是为了演示连接SQL语句,当然实际开发中我们不会这样建表,实际开发中这两个表会有自己不同的主键。) 一、外连接 外连接可分为:左连接、右连接、完全外连接。 1、左连接  left join 或 left outer join SQL语句:select * from...

经验教程

800

收藏

15
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部