您好,欢迎来到投聚财经。
搜索
您的当前位置:首页Sybase的数据操纵语言

Sybase的数据操纵语言

来源:投聚财经




Sybase的数据操纵语言

1Select语句

基本语法:
SELECT[all|distinct]字段列表
[into表名]
[from表名]
[where条件表达式]
[groupby [all]字段列表]
[having筛选表达式]
[orderby 字段列表[asc|desc]]
[compute聚集函数列表[by字段列表]]
注意:
Select语句中的子句必须按照上述顺序使用。也就是说,若该语句包括一个groupby子句和一个orderby子句wheregroupby子句必须放在orderby子句之前。

Having子句类似于where子句,不同之处有两点:(1Having子句必须结合groupby子句使用;(2where子句不能用聚集函数,而Having子句可以。

下面通过实例来对Select的通常用法加以介绍。

1:选择所有的列,语法为select*from table_list如:select * frompublishers

from table_name 如:selectpub_id,pub_name from publishers

3:重命名查询结果中的列,语法为
selectcolumn_heading= column_name
fromtable_name
如:selectPublisher=pub_name,pub_id
frompublishers

4select列表中的计算值,可以对select列表中的数值数据进行计算,下面列出了算术运算符。

符号运算
+
-

/
*
%取模






selecttitle_id,total_sales,total_sales*2 from titles

5:使用distinct消除重复的查询结果
可选的关键词消除select语句的结果中的重复行。若不指定distinct,缺省值为all,将检索出包含重复行的所有行数据。

如:selectdistinct au_id from titleauthor

6:选择行——where语句
select语句中的确切指定要检索哪些行的准则,其一般格式为:selectselect_list from table_list where search_conditions
where子句中的搜索条件(或称)包括:
·比较运算符(=,<,>,!=等=
如:whereadvance*2total_sales*price
·范围(betweennot between
如:wheretotal_sales between 5000 and 10000
·列表(innot in
如:wherestate in(“CA”,”IN”,”MD”)
·匹配字符(likenot like
如:wherephone like “0535%”
·未知值(isnull is not null
如:whereadvance is null·以上各项的组合(and,or)如:where advance5000or total_sales between 500 and 1000

集合函数结果Sum([all|distinct]expression)数值列中(不重复)值的总和
Avg([all|distinct]expression)数值列中(不重复)值的平均
count([all|distinct]expression)列中(不重复)非空值的数目
Count(*)选定的行数
Max(expression)Expression的最大值
Min(expression)Expression的最小值

如:selectavg(advance),sum(total_sales)
fromtitles
wheretype=”as”
selectcount(*) from titles
selectavg(distinct price) from titles
selectmax(price) from books

8:分组组织查询结果——group by 子句
group by 子句用在select 语句中将一张表分成若干组。

如:select type, advance from titles group by type




9:选择分组数据——having子句
havinggroupby 子句设置条件,与whereselect语句设置条件一样。Having搜索条件与where相同,但having可包括集合函数,而where不能包括。

下列语句使用带集合函数having子句的例子。它把title表中的行按类型分组,但去掉了那只包含一本书的分组。

Selecttype from titles group by type having count(*)>1
下面是一个不带集合函数的having子句的例子。它把title表中的行按类型分组,但去掉了那些不以字母“p开头的类型。

Selecttype from titles group by type having type like “p%”

10:查询结果排序——orderby子句
Orderby子句允许按一列或多列对查询结果排序。每个排序可以是升序的(asc)或降序的(desc)。若不特别指明,则按升序进行。下列查询返回按pub_id排序的结果:
Selectpub_id,type,title_id from titles order by pub_id

11:连接——从多张表中检索数据
连接两张或两张以上的表是这样一个过程:比较指定字段中的数据,根据比较结果用符合条

件的行组成一张新表。

select publishers.pub_id,publishers.pub_name,authors.* frompublishers,authors where publishers.city=authors.city 举例:

句。例如: Selecttype,price,advance
From titles
Order by type
Computesum(price),sum(advance) by type

2Insert语句

Insert命令向数据库中添加行有两种方法:使用关键词values或使用select语句。

Insert语句的基本语法为:
Insert[into]表名[(字段列表)]
{values(值列表)|select_statement}
举例:
insertinto publishers
values(‘1622’,’Jardin,Inc.’,’Camden’,’NJ’)
Insertinto publishers(pub_id,pub_name)

values(‘1756’,’The Health Center’)
Insert authors select * from newauthors
Insert authors(au_id,address,au_lname,au_fname)





Select* from newauthors

3Delete语句

Delete可以对一行或多行进行操作。

Delete语句的基本语法为:
Delete表名
[from表名列表]
[where条件表达式]
举例:Deletepublishers
wherepub_name=”Jardin,Inc.”
Deletetitles
Fromauthors, titles
Wheretitles.title_id=authors.title_id

4Update语句

可以使用Update命令来改动表中的单个行、一组行或所有行。

Update 语句的基本语法为:
Update表名
Setcolumn_name1={expression1|null|(select_statement)}
[,column_name2={expression2|null|(select_statement)}]
[……][from 表名列表] [where 条件表达式]

update titles 举例:

settotal_sales=total_sales qty
fromtitles,sales
wheretitles.title_id=sales.title_id



Copyright © 2019- tjwe.cn 版权所有

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务