例9:选择分组数据——having子句 having为group by 子句设置条件,与where为select语句设置条件一样。Having搜索条件与where相同,但having可包括集合函数,而where不能包括。 下列语句使用带集合函数having子句的例子。它把title表中的行按类型分组,但去掉了那只包含一本书的分组。 Select type from titles group by type having count(*)>1 下面是一个不带集合函数的having子句的例子。它把title表中的行按类型分组,但去掉了那些不以字母“p”开头的类型。 Select type from titles group by type having type like “p%” 例10:查询结果排序——order by子句 Order by子句允许按一列或多列对查询结果排序。每个排序可以是升序的(asc)或降序的(desc)。若不特别指明,则按升序进行。下列查询返回按pub_id排序的结果: Select pub_id,type,title_id from titles order by pub_id 例11:连接——从多张表中检索数据 连接两张或两张以上的表是这样一个过程:比较指定字段中的数据,根据比较结果用符合条件的行组成一张新表。 举例: select publishers.pub_id,publishers.pub_name,authors.* from publishers,authors where publishers.city=authors.city 例12:分组计算子句 Compute是Sybase对SQL标准中Group子句的扩充,可以将其看作带聚集计算的Group子句。例如: Select type,price,advance From titles Order by type Compute sum(price),sum(advance) by type 2.Insert语句 用Insert命令向数据库中添加行有两种方法:使用关键词values或使用select语句。 Insert语句的基本语法为: Insert[into]表名[(字段列表)] {values(值列表)|select_statement} 举例:insert into publishers values(‘1622’,’Jardin,Inc.’,’Camden’,’NJ’) Insert into 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 3.Delect语句 Delect可以对一行或多行进行操作。 Delect语句的基本语法为: Delect 表名 [from 表名列表] [where条件表达式] 举例:Delect publishers where pub_name=”Jardin,Inc.” Delect titles From authors, titles Where titles.title_id=authors.title_id 4.Update语句 可以使用Update命令来改动表中的单个行、一组行或所有行。 Update语句的基本语法为: Update表名 Set column_name1={expression1|null|(select_statement)} [,column_name2={expression2|null|(select_statement)}] [……] [from 表名列表] [where 条件表达式] 举例: update authors set_au_lname=”Health”,aufname=”Goodbody” where au_lname=”Bloth” update titles set total_sales=total_sales + qty from titles,sales where titles.title_id=sales.title_id