--测试数据
if object_id('b') is not null drop table b
go
create table b (id int, size float)
insert b select 1, 3.5
union all select 2, 4.5
union all select 3, 5
union all select 4, 7
union all select 5, 8.5
if object_id('c') is not null drop table c
go
create table c(color varchar(10),
[3] int,[3.5] int,[4] int,
[4.5] int,[5] int,[5.5] int,
[6] int,[6.5] int,[7] int,
[7.5] int,[8] int,[8.5] int)
insert c select 'color1', 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0
union all select 'color2', 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0
union all select 'color3', 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0--查询
declare @str varchar(100)
set @str='select color '
select @str=@str+',['+convert(varchar(5), size)+']'
from b
where size in(select name from syscolumns
where id=object_id('c'))
set @str=@str+' from c'
--print @str
exec(@str)
if object_id('b') is not null drop table b
go
create table b (id int, size float)
insert b select 1, 3.5
union all select 2, 4.5
union all select 3, 5
union all select 4, 7
union all select 5, 8.5
if object_id('c') is not null drop table c
go
create table c(color varchar(10),
[3] int,[3.5] int,[4] int,
[4.5] int,[5] int,[5.5] int,
[6] int,[6.5] int,[7] int,
[7.5] int,[8] int,[8.5] int)
insert c select 'color1', 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0
union all select 'color2', 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0
union all select 'color3', 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0--查询
declare @str varchar(100)
set @str='select color '
select @str=@str+',['+convert(varchar(5), size)+']'
from b
where size in(select name from syscolumns
where id=object_id('c'))
set @str=@str+' from c'
--print @str
exec(@str)
如果你想要一句话来实现你的功能, 那就比较麻烦了.不会,看高手吧.
where C.COLOR in
(
select SIZE from B
)