SQL基础第二章从表中获取信息

来源:互联网 发布:竞品分析的数据来源 编辑:程序博客网 时间:2024/05/18 03:55
<html><p align="center"><font color="#FF0000" size="7">第二章     从表中获取信息</font></p><p align="center"><font color="#FF0000" size="4"> <strong>Select语句</strong></font></p><p><font size="4"> 在SQL中,select语句用于从表中获取信息,。它有六个子句:</font></p><p><font size="4"> select   要获取哪几列数据</font></p><p><font size="4">from     哪个表中有数据</font></p><p><font size="4">where    获得哪几行数据</font></p><p><font size="4"> group by     having    </font></p><p><font size="4">order  by 使用哪几列来对结果进行归类这些语句的使用是有一定顺序的,group by和having用于数据的总结。</font></p><p><font size="4">后面会有介绍,。</font></p><p><font size="4"> from语句总是只列出一个表</font></p><p><font size="4">一个select语句通常被称为一个查询,</font></p><p><font size="4"> 这里给出了使用上述所有子句的select语句的一个例子:</font></p><p><font size="4"><strong>显示Lunches数据库的1_employees表中的employee_id,last-name,credit_limit,列,只显示信贷额超过¥20.00的员工。按员工的last_name对结果进行排序。</strong></font></p><p><font color="#0000FF" size="4">select employee_id,</font></p><p><font color="#0000FF" size="4"> last_name;</font></p><p><font color="#0000FF" size="4">credit_limit</font></p><p><font color="#0000FF" size="4">from      1_employees</font></p><p><font color="#0000FF" size="4"> where      credit_limit>20.00</font></p><p><font color="#0000FF" size="4"> order by   last_name;  </font></p><p align="center"><strong><font color="#FF0000" size="4">select子句  </font></strong></p><p><font size="4">select子句在结果表中列出了所有显示的列,列名后如果没有逗号,计算机就认为下面将出现另一个子句,即from子句。</font><font size="4">还要注意,这些列名不能含有空格。</font></p><p><font size="4"> where子句表时在结果中显示哪几行,</font></p><p><font size="4">order by子句指定结果表的行必须按照子母顺序进行排列,分号表明语句结束。</font></p><p><font size="4"> 根据关系数据库理论,表是惟一的数据库结构,所以我将查询结果清单称为结果表,结果表只出现在屏幕上,并不存储在硬盘上。</font></p><p><font size="4"> select子句是查询的第一部分,一共有三种形式:</font></p><p><font size="4"> 1.select 列:  只获取列出的列,按这些列被列出的顺序放置这些列,可以重新命名这些列。</font></p><p><font size="4"> 2.select *或select 表名. *  获取表中所有的列,排列顺序与表中顺序相同,不能够重新命名这些列。</font></p><p><font size="4"> 3. select distinct 列:  只获取列出的列,按列出这些列的顺序放置这些列,可以重新命名这些列,除去结果中的重复行。</font></p><p><font size="4"> select子句和from子句是必须的,可以使用这两个子句写一条select语句。  select *  from 1_employees.        使用select子句获取若干列    多次列出同一个列是可能的。对列使用不同的格式或者函数时,这样做是非常有用的。  select子句中可以包含文字值,这个值会出现在结果表的每一行,如果文字值是文本或日期,则必须使用单引号将其括起来。如果是数字,就不需要使用引号。  通过指定别名,可以对列进行重新命名。</font></p><p><font size="4">其语法如下:  column_name As alias_name  As的使用。有时候为了节省空间,列标题在结果表中会被缩写,而不是显示列的全名或别名。</font></p><p><font size="4"> <strong>任务:从1_employees中取出三列:employee_id;phone_number;last_name;  按照上面的顺序显示它们,将employee_id的名字改为employee_number,  phone_number的名字改为extension.  创建两个新列,evaluation和rating.每个员工的评定(evaluation)都为“excellent worker“,等级为10.</strong></font></p><p><font size="4"> 方法:</font></p><p><font size="4"> select employee_id as employee_number,</font></p><p><font size="4"> phone_number as extension,</font></p><p><font size="4"> last_name,  </font></p><p><font size="4">‘excellent worker'as evaluation,  </font></p><p><font size="4">10 as rating</font></p><p><font size="4"> from 1_employees;</font></p><p><font size="4"> 文本“excellent worker"被添加到名为evaluation列中的每一行,这是在select语句中放置文字的一个例子。这个例子的文字值使用的是文 本,因此使用单引号将其括了引起来。如果是数字,则不需要将它括起来。</font></p><p><font size="4"> 使用select子句获取所有的列  select *  from 1_employees;</font></p><p><font size="4"> 使用select语句获取某个列中的相异值  select distinct</font></p><p><font size="4"> select distinct manage_id  from 1_employees;  select distinct可以使用where子句限制结果表中的行数。where子句最先进行处理,它从原表中删除了一些行,然后再运行select distinct.      </font></p><p><font size="4">where子句概述  where 中的条件有:  等于----和其它比较测试  =   <    >    <=    >=    <>不等于  包含测试------特定值的清单  in    not in   like   not like   between  not between  is null    is not null  and   or   not</font></p><p align="center"><font color="#FF0000" size="4"> 在where中使用等于条件</font></p><p><font size="4"> select employee_id,  </font></p><p><font size="4">first_name,  </font></p><p><font size="4">last_name,  </font></p><p><font size="4">manage_id  </font></p><p><font size="4">from   1_employees  </font></p><p><font size="4">where manage_id=203  or manager_id is null;  </font></p><p><font size="4">where manage_id<222;  where not (manager_id = 203);  where manager_id<>203;  where manager_id !=203;  要想从原表中显示所有这些行,需要考虑三种情况:  where manager_id=203  where manager_id!=203  where manager_id is null;  </font></p><p><font color="#FF0000" size="4">在where中使用in条件</font></p><p><font size="4"> where manager_id in(202,203);</font></p><p><font color="#FF0000" size="4"> 在where中使用between语句</font></p><p><font size="4"> between语句条件可以应用于数字、文本和日期,在这个例子中,它被应用于日期。在Oracle中,日期必须是使用单引号引起来的。在Access中,日期必须包含在井字符中。  </font></p><p><font size="4">where hire_data between'16-aug-1999'  and '01-jul-2003';  where hire_data between #16-aug-1999#  and#       #;  </font></p><p><font color="#FF0000" size="4">在where中使用like条件  </font></p><p><font size="4">like条件被用于数据的查找模式,这些模式是使用通配符指定的。这只用于like条件,当对另一个条件使用同一个通配符时它们不再是通配符。  模式指定应该在单引号中进行。</font></p><p><font size="4"> 通配符和他们的含义:  在Access中:  *  任意长度的字符串,或者可能根本就没有字符。  ? 一个字符。  # 一个数字。  【c-m】字符的范围  [!c-m]字符范围之外  [*][#]将字符放入方括号意味着按照字面意思使用它。而不是将它和作通配符。  where last_name like '*n*';  意思是查找出其中含有字母n的单词。  在where中使用is null  where manager_id is null;      </font></p><p><font color="#FF0000" size="4">order by子句概述</font></p><p><font size="4"> 默认值是升序排序,通常不指定它。为了降序排列,则必须指定desc.  列通过它的名字指定的。  语法::  order by列名  可以为每一个指定一个排序顺序  每一个列的排序选项:  asc  升序 默认  desc   降序    例如:  </font></p><p><font size="4">order by employee_id  </font></p><p><font size="4">order by last_name,first_name  </font></p><p><font size="4">order by hire_date desc,</font></p><p><font size="4"> last_name,  first_name      通过若干列升序排列行 </font></p><hr align=left width=80% size=2 noshade><a href="D:\SQL\SQL基础\首页.html"><font color=green size=7><strong><u>返回首页</u></strong></font></a></html>


0 0
原创粉丝点击