Transact-SQL基本的数据访问

1、表中列的使用技法如下:
1) 选择所有列
 可以使用(*)来表示表中所有列
2) 选择部分列
Use pubs
Select au_lname,au_fname,phone,address from authors

3) 为字段设置别名
Use pubs
Select title “书名”,price “价格” from tiltles  /*替代名带双引号*/
Select title 书名,price 价格 from titles  /*替代名不带引号*/
Select title ‘书名’,price ‘价格’ from titles  /*替代名带单引号*/
Select title as 书名,price as 价格 from titles  /*使用as关键字*/

4) 在选择列表中使用表达式
Select title as 书名, price as 原价, price*0.5 as 现价 from titles
5) 消除字段数据的重复项
Select distinct type as 分类from titles
6) 限制记录的行数
使用top关键字
Select top n [percent] * from 表名
使用Set rowcount语句
Set rowcount 4
Select * from authors
Select * from titles
Set rowcount 0
Select * from authors
Select * from titles
2、From子句使用的技法如下:
1) 指定一个或多个表
Use pubs
Select * from titles
Use pubs/*错误*/
Select * from titles and authors
2) 在多个表之间联结
Select titles.title, titleauthor,au_ord,authors.au_lname,
Titles.price,titles.ytd_sales.titles.pub_id
Form authors inner join
titleauthor on authors.author.au_id = titleauthor.au_id inner join
Titles on titleauthor.titles_id = titles.titles_id
3)  使用派生表
3、条件子查询
1) 条件子句
use pubs
select stores.stor_id as 书店编号, stores.stor_name as 书店名称
from stores,(select stored, count(distinct title_id) as title_count
      from sales group by stor_id) as stored
where stores.stor_id = stored.stor_id and stor_id.title_count =
(select count(*) from titles)
 
2) 使用比较运算符
select title_id as 书号,title as 书名,price as 价格 from titles
where price>15

3) 使用逻辑运算符
4) 使用Between搜索条件
5) 使用In列表搜索条件
6) 使用Like匹配模式
7) 空值比较搜索条件
4、排序和分组查询技法如下:
1) 排序查询技法
2) 数据记录分组技法


二〇〇六年五月二十三日

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值