--那么你的要求是这样 aa.fileid cc.text1 bb.text 1 g A 1 h B 1 i C --但同样的fileid=1 为什么不允许他这样 aa.fileid cc.text1 bb.text 1 g B 1 h C 1 i A --或这样aa.fileid cc.text1 bb.text 1 g C 1 h A 1 i B 你肯定有你的规则吧,说一下你的规则呢
希望按表的写入顺序,且BB表CC表各有一个自增量字段。谢谢
--有自增字段就好办了 select field=isnull(a.field,b.field),b.[text1],a.[text] from( select aa.fileid,bb.[text],sid=( select count(*) from bb b where fileid=aa.fileid and b.自增字段<=bb.自增字段) from aa,bb where aa.filetype=bb.fileid )a full join( select aa.fileid,cc.[text1],sid=( select count(*) from cc c where fileid=aa.fileid and c.自增字段<=cc.自增字段) from aa,cc where aa.filetype=cc.fileid )b on a.fileid=b.fileid and a.sid=b.sid
--测试数据 create table aa(number int,fileid int,filetype int) insert aa select 1,1, 1 union all select 2,2, 3create table bb(自增字段 int identity,fileid int,[text] varchar(10)) insert bb select 1,'A' union all select 1,'B' union all select 1,'C' union all select 2,'D' union all select 2,'E' union all select 2,'F'create table cc(自增字段 int identity,fileid int,text1 varchar(10)) insert cc select 1,'g' union all select 1,'h' union all select 1,'i' go--查询处理 select fileid=isnull(a.fileid,b.fileid),b.[text1],a.[text] from( select aa.fileid,bb.[text],sid=( select count(*) from bb b where fileid=aa.fileid and b.自增字段<=bb.自增字段) from aa,bb where aa.filetype=bb.fileid )a full join( select aa.fileid,cc.[text1],sid=( select count(*) from cc c where fileid=aa.fileid and c.自增字段<=cc.自增字段) from aa,cc where aa.filetype=cc.fileid )b on a.fileid=b.fileid and a.sid=b.sid go--删除测试 drop table aa,bb,cc/*--测试结果fileid text1 text ----------- ---------- ---------- 1 g A 1 h B 1 i C(所影响的行数为 3 行) --*/
aa.fileid cc.text1 bb.text
1 g A
1 h B
1 i C --但同样的fileid=1 为什么不允许他这样
aa.fileid cc.text1 bb.text
1 g B
1 h C
1 i A --或这样aa.fileid cc.text1 bb.text
1 g C
1 h A
1 i B
你肯定有你的规则吧,说一下你的规则呢
select field=isnull(a.field,b.field),b.[text1],a.[text]
from(
select aa.fileid,bb.[text],sid=(
select count(*) from bb b
where fileid=aa.fileid and b.自增字段<=bb.自增字段)
from aa,bb
where aa.filetype=bb.fileid
)a full join(
select aa.fileid,cc.[text1],sid=(
select count(*) from cc c
where fileid=aa.fileid and c.自增字段<=cc.自增字段)
from aa,cc
where aa.filetype=cc.fileid
)b on a.fileid=b.fileid and a.sid=b.sid
create table aa(number int,fileid int,filetype int)
insert aa select 1,1, 1
union all select 2,2, 3create table bb(自增字段 int identity,fileid int,[text] varchar(10))
insert bb select 1,'A'
union all select 1,'B'
union all select 1,'C'
union all select 2,'D'
union all select 2,'E'
union all select 2,'F'create table cc(自增字段 int identity,fileid int,text1 varchar(10))
insert cc select 1,'g'
union all select 1,'h'
union all select 1,'i'
go--查询处理
select fileid=isnull(a.fileid,b.fileid),b.[text1],a.[text]
from(
select aa.fileid,bb.[text],sid=(
select count(*) from bb b
where fileid=aa.fileid and b.自增字段<=bb.自增字段)
from aa,bb
where aa.filetype=bb.fileid
)a full join(
select aa.fileid,cc.[text1],sid=(
select count(*) from cc c
where fileid=aa.fileid and c.自增字段<=cc.自增字段)
from aa,cc
where aa.filetype=cc.fileid
)b on a.fileid=b.fileid and a.sid=b.sid
go--删除测试
drop table aa,bb,cc/*--测试结果fileid text1 text
----------- ---------- ----------
1 g A
1 h B
1 i C(所影响的行数为 3 行)
--*/
使用指定的替换值替换 NULL。语法
ISNULL ( check_expression , replacement_value ) 参数
check_expression将被检查是否为 NULL的表达式。check_expression 可以是任何类型的。replacement_value在 check_expression 为 NULL时将返回的表达式。replacement_value 必须与 check_expresssion 具有相同的类型。 返回类型
返回与 check_expression 相同的类型。注释
如果 check_expression 不为 NULL,那么返回该表达式的值;否则返回 replacement_value。