大家先看这两个表
表1
cpbm bm1 bm2 bm3 bm4 bm5 bm6 ---->表头
xxxxx 1111 2222 3333 4444 5555 6666----->以下是数据
aaaas 1111 2121 2121 0210 1111 0120
sadsa 1251 2541 2541 25441 2541 2541
表2
bm name shuliang gegui xingzhuang dengji
1111 asd 2 25*2 hao 1
2222 sda 2 25*3 hao 2
0210 sad 3 10*2 liang 1
2541 sas 2 20*2 hao 1
3333 asd 3 23*1 hao 2
2121 ads 2 255*1 hao 2
假如我搜索"xxxxx" 就会输出表3:
bm name shuliang gegui xingzhuang dengji
1111 asd 2 25*2 hao 1
2222 sda 2 25*3 hao 2
3333 asd 3 23*1 hao 2
4444 .......
5555 ......
6666 ......
不知道大家明白我的意思吗 ?
就是我给出搜索条件 然后搜索表1 再将表1的得到的结果在表2中分别搜索 最后将所有的结果 如表3一样输出
表1
cpbm bm1 bm2 bm3 bm4 bm5 bm6 ---->表头
xxxxx 1111 2222 3333 4444 5555 6666----->以下是数据
aaaas 1111 2121 2121 0210 1111 0120
sadsa 1251 2541 2541 25441 2541 2541
表2
bm name shuliang gegui xingzhuang dengji
1111 asd 2 25*2 hao 1
2222 sda 2 25*3 hao 2
0210 sad 3 10*2 liang 1
2541 sas 2 20*2 hao 1
3333 asd 3 23*1 hao 2
2121 ads 2 255*1 hao 2
假如我搜索"xxxxx" 就会输出表3:
bm name shuliang gegui xingzhuang dengji
1111 asd 2 25*2 hao 1
2222 sda 2 25*3 hao 2
3333 asd 3 23*1 hao 2
4444 .......
5555 ......
6666 ......
不知道大家明白我的意思吗 ?
就是我给出搜索条件 然后搜索表1 再将表1的得到的结果在表2中分别搜索 最后将所有的结果 如表3一样输出
union all
select 表2.* from 表2 ,表1 where 表2.bm=表1.bm2 where 表1.cpbm='xxxxx'
union all
select 表2.* from 表2 ,表1 where 表2.bm=表1.bm3 where 表1.cpbm='xxxxx'
union all
select 表2.* from 表2 ,表1 where 表2.bm=表1.bm4 where 表1.cpbm='xxxxx'
union all
select 表2.* from 表2 ,表1 where 表2.bm=表1.bm5 where 表1.cpbm='xxxxx'
union all
select 表2.* from 表2 ,表1 where 表2.bm=表1.bm6 where 表1.cpbm='xxxxx'
(select
cast(bm1 as varchar(10))+','
+cast(bm2 as varchar(10))+','
+cast(bm3 as varchar(10))+','
+cast(bm4 as varchar(10))+','
+cast(bm5 as varchar(10))+','
+cast(bm6 as varchar(10)) from 表1 where cpbm= 'xxxxx' ),bm)>0
(select
cast(bm1 as varchar(10))+','
+cast(bm2 as varchar(10))+','
+cast(bm3 as varchar(10))+','
+cast(bm4 as varchar(10))+','
+cast(bm5 as varchar(10))+','
+cast(bm6 as varchar(10)) from 表1 where cpbm= 'xxxxx' ))>0
--cpbm bm1 bm2 bm3 bm4 bm5 bm6 ---->表头
--xxxxx 1111 2222 3333 4444 5555 6666----->以下是数据
--aaaas 1111 2121 2121 0210 1111 0120
--sadsa 1251 2541 2541 25441 2541 2541
--表2
--bm name shuliang gegui xingzhuang dengji
--1111 asd 2 25*2 hao 1
--2222 sda 2 25*3 hao 2
--0210 sad 3 10*2 liang 1
--2541 sas 2 20*2 hao 1
--3333 asd 3 23*1 hao 2
--2121 ads 2 255*1 hao 2 if object_id('表1') is not null drop table 表1
go
create table 表1 (cpbm varchar(30),bm1 varchar(10),bm2 varchar(10),bm3 varchar(10),bm4 varchar(10),bm5 varchar(10),bm6 varchar(10))
insert into 表1 select 'xxxxx','1111','2222','3333','4444','5555','6666' union all
select 'aaaas','1111','2121','2121','0210','1111','0120'
select * from 表1
goif object_id('表2') is not null drop table 表2
create table 表2(bm varchar(10),name varchar(10),shuliang int,gegui varchar(20),xingzhuang varchar(20),dengji int)
insert into 表2 select '1111','asd',2,'25*2','hao',1 union all
select '1111','asd',2,'25*2','hao',1 union all
select '2222','asd',2,'25*2','hao',1 union all
select '3333','asd',2,'25*2','hao',1 union all
select '2121','asd',2,'25*2','hao',1 union all
select '0210','asd',2,'25*2','hao',2select * from 表2declare @sqlstr varchar(8000)
select @sqlstr=''
select @sqlstr=@sqlstr+ ' union all select '+[name]+' from 表1 where cpbm='''+N'xxxxx'+''''+char(10)
from syscolumns where id=object_id('表1') and [name]<>'cpbm'
select @sqlstr='select * from 表2 where bm in('+substring(@sqlstr,11,len(@sqlstr))+')'
exec @sqlstr
--A 3 3
--B 1 2
--C 2 1
--表1
--cpbm bm1 bm2 bm3 bm4 bm5 bm6 ---->表头
--xxxxx 1111 2222 3333 4444 5555 6666----->以下是数据
--aaaas 1111 2121 2121 0210 1111 0120
--sadsa 1251 2541 2541 25441 2541 2541
--表2
--bm name shuliang gegui xingzhuang dengji
--1111 asd 2 25*2 hao 1
--2222 sda 2 25*3 hao 2
--0210 sad 3 10*2 liang 1
--2541 sas 2 20*2 hao 1
--3333 asd 3 23*1 hao 2
--2121 ads 2 255*1 hao 2 if object_id('表1') is not null drop table 表1
go
create table 表1 (cpbm varchar(30),bm1 varchar(10),bm2 varchar(10),bm3 varchar(10),bm4 varchar(10),bm5 varchar(10),bm6 varchar(10))
insert into 表1 select 'xxxxx','1111','2222','3333','4444','5555','6666' union all
select 'aaaas','1111','2121','2121','0210','1111','0120' go if object_id('表2') is not null drop table 表2
create table 表2(bm varchar(10),name varchar(10),shuliang int,gegui varchar(20),xingzhuang varchar(20),dengji int)
insert into 表2 select '1111','asd',2,'25*2','hao',1 union all
select '1111','asd',2,'25*2','hao',1 union all
select '2222','asd',2,'25*2','hao',1 union all
select '3333','asd',2,'25*2','hao',1 union all
select '2121','asd',2,'25*2','hao',1 union all
select '0210','asd',2,'25*2','hao',2
go
declare @sqlstr varchar(8000)
select @sqlstr=''
select @sqlstr=@sqlstr+ ' union all select '+[name]+' from 表1 where cpbm='''+N'aaaas'+''''+char(10)
from syscolumns where id=object_id('表1') and [name] <>'cpbm'
select @sqlstr='select distinct * from 表2 where bm in('+substring(@sqlstr,11,len(@sqlstr))+')'
exec(@sqlstr)
go
表1
cpbm bm1 sl1 bm2 sl2 bm3 sl3 bm4 sl4 bm5 sl5 bm6 sl6
xxxxx 1111 2 2222 2 3333 3 4444 1 5555 1 6666 1
aaaas 1111 3 2121 2 2121 2 0210 1 1111 1 0120 1
sadsa 1251 2 2541 2 2541 1 2544 1 2541 1 2541 2
表2
bm name gegui xingzhuang dengji
1111 asd 25*2 hao 1
2222 sda 25*3 hao 2
0210 sad 10*2 liang 1
2541 sas 20*2 hao 1
3333 asd 23*1 hao 2
2121 ads 255*1 hao 2
假如我搜索"xxxxx" 就会输出表3:
bm name sl gegui xingzhuang dengji
1111 asd 2 25*2 hao 1
2222 sda 2 25*3 hao 2
3333 asd 3 23*1 hao 2
4444 ....... 1
5555 ...... 1
6666 ...... 1
注释:表1的 CPBM为成品编码 BM为编号 SL为数量
表2 BM为编号
输出表3中 BM为编号 SL为数量 字段不用排列 只要结果
就是我给出搜索条件 然后搜索表1 再将表1的得到的结果在表2中分别搜索 最后将所有的结果 如表3一样输出
谢谢大家的热心帮助
当表1 中cpbm不唯一时,即count(cpbm)>1 when cpbm='xxxxx' ,会出现“Server: Msg 512, Level 16, State 1, Line 4128
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
”的提示。
注释:表1的 CPBM为成品编码(唯一的) BM为编号 SL为数量
表2 BM为编号(唯一的)