t_sql查询步骤详解

                  --==testdata==
                 --======徐王锦======
                --==2009/01/15/pm 23:19====
            --======关于查询语句的顺序======
--
test data
if object_id(N'company','U') is not null
drop table company;
if object_id(N'customer','U') is not null
drop table customer;
create table company
(id
smallint identity(1,1),
company_name
varchar(50),
cp_code
tinyint
);
create table  customer
(id
smallint identity(1,1),
cp_code
tinyint,
customer
varchar(100),
list_code
char(10)
);
insert into company(company_name,cp_code)
select N'mic',1
union all
select N'lexian',2
union all
select N'weiyin',3
union all
select N'sdc',4
;
insert into customer(cp_code,customer,list_code)
select 1,N'wangjinxu',N'0801010001'
union all
select 1,N'xiaoqiuye',N'0801010002'
union all
select 1,N'weihaohe',N'0801010003'
union all
select 2,N'wangjinxu',N'070910001'
union all
select 2,N'jiayicao',N'070910002'
union all
select 3,N'wangjinxu',N'1109010001'
;
go

--要求 wangjinxu 的订单信息
select
from customer as a
left outer join company as b
on a.cp_code=b.cp_code
where a.customer=N'wangjinxu'

--action 1
生成 vt1(虚拟表)
from customer as a  ....join company as b
进行笛卡尔积(
cross join)
--vt1
/*

id cp_code  customer    list_code  id  company_name cp_code
1    1        wangjinxu    0801010001    1    mic            1
2    1        xiaoqiuye    0801010002    1    mic            1
3    1        weihaohe    0801010003    1    mic            1
4    2        wangjinxu    070910001     1    mic            1
5    2        jiayicao    070910002     1    mic            1
6    3        wangjinxu    1109010001    1    mic            1
1    1        wangjinxu    0801010001    2    lexian        2
2    1        xiaoqiuye    0801010002    2    lexian        2
3    1        weihaohe    0801010003    2    lexian        2
4    2        wangjinxu    070910001     2    lexian        2
5    2        jiayicao    070910002     2    lexian        2
6    3        wangjinxu    1109010001    2    lexian        2
1    1        wangjinxu    0801010001    3    weiyin        3
2    1        xiaoqiuye    0801010002    3    weiyin        3
3    1        weihaohe    0801010003    3    weiyin        3
4    2        wangjinxu    070910001     3    weiyin        3
5    2        jiayicao    070910002     3    weiyin        3
6    3        wangjinxu    1109010001    3    weiyin        3
1    1        wangjinxu    0801010001    4    sdc            4
2    1        xiaoqiuye    0801010002    4    sdc            4
3    1        weihaohe    0801010003    4    sdc            4
4    2        wangjinxu    070910001     4    sdc            4
5    2        jiayicao    070910002     4    sdc            4
6    3        wangjinxu    1109010001    4    sdc            4
*/

--action2
on a.cp_code=b.cp_code
在 vt1 的基础上进行筛选生成 vt2
/*
id cp_code  customer    list_code  id  company_name cp_code
1    1        wangjinxu    0801010001    1    mic            1
2    1        xiaoqiuye    0801010002    1    mic            1
3    1        weihaohe    0801010003    1    mic            1
4    2        wangjinxu    070910001     2    lexian        2
5    2        jiayicao    070910002     2    lexian        2
6    3        wangjinxu    1109010001    3    weiyin        3
*/

--action3
因为是outer join  把保留表 customer 中未找到匹配的行作为外部行添加到vt2 生成vt3
(未匹配的column_value 用null填空)
如果
from 后的子句多于2个表 则前2个表生成的结果集和第三个表重新进行1-3步骤
更多的以此类推.
--action 4
where a.customer=N'wangjinxu'
把 vt3 根据
where ...条件筛选生成vt4
因此
outer join 中 最后的筛选条件是在保留表(左输入)筛选 放在 where  如果在 on 里面筛选
到 action3 还会把未匹配的保留表的行做为外部输入加入到 vt2
--待续

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值