创建视图sql关联产品的三级类别
分类:数据库| 发布:camnprbubuol| 查看:409 | 发表时间:2011/1/24
产品分三级类别,但有的只有二级类别,现在想用一个视图,将所有的产品和类别关联起来,有三级类的就显示,没有三级类的只显示一二级类名称,三级类别处显示为null
11 | threeid twoid threefolder |
15 | id oneid twoid threeid proname |
19 | id proname onefolder twofolder threefolder |
20 | 1 产品1 testa testb testc |
21 | 2 产品2 testa1 testb2 此处是 null |
22 | 注意一下product表的数据,因为产品2只能二级类别,没有三级,所以第三级类别值为0 |
03 | create table A(oneid int ,onefolder varchar (20)) |
04 | insert into A select 50 ,’testa’ |
05 | insert into A select 53 ,’testa1’ |
07 | create table B(twoid int ,oneid int ,twofolder varchar (20)) |
08 | insert into B select 87 ,50,’testb’ |
09 | insert into B select 88 ,50,’testb1’ |
10 | insert into B select 89 ,53,’testb2’ |
12 | create table C(threeid int ,twoid int ,threefolder varchar (20)) |
13 | insert into C select 12 ,87 ,’testc’ |
14 | insert into C select 13 ,88 ,’testc1’ |
16 | create table product(id int ,oneid int ,twoid int ,threeid int ,proname varchar (20)) |
17 | insert into product select 1,50,87,12,’产品1’ |
18 | insert into product select 2,53 ,89 ,0 ,’产品2’ |
24 | select x.id, x.proname,onefolder,twofolder,threefolder |
25 | from product x left join A on x.oneid=A.oneid |
26 | left join B on x.twoid=B.twoid |
27 | left join C on x.threeid=C.threeid |
33 | drop table A,B,C,product |
36 | id proname onefolder twofolder threefolder |
38 | 1 产品1 testa testb testc |
39 | 2 产品2 testa1 testb2 NULL |