ID xkm fuhao tape
1 xk1 a 1s
2 xk2 b 2s
3 xk3 a 1s
4 xk4 b 1s
怎么通过fuhao中的相同字段生成多张表,如上面fuhao字段有两个相同的内容:a、b,则生成两张表
表1:
ID xkm fuhao tape
1 xk1 a 1s
2 xk3 a 1s
表2:
ID xkm fuhao tape
1 xk2 b 2s
2 xk4 b 1s
如果fuhao中有3个则拆分成3个新表,请大家帮忙,我是个新手,在做一个网站,连接mysql数据库,很多不懂,求助
7 个解决方案
#1
建议去MYSQL版.
#2
create table tb(ID int,xkm varchar(3),fuhao varchar(1),tape varchar(2))
insert into tb
select 1,'xk1','a','1s' union all
select 2,'xk2','b','2s' union all
select 3,'xk3','a','1s' union all
select 4,'xk4','b','1s'
select 'select * into #'+ltrim(row_number() over (order by fuhao))
+' from tb where fuhao='''+fuhao+'''' from tb
group by fuhao
/*
select * into #1 from tb where fuhao='a'
select * into #2 from tb where fuhao='b'
*/
思路上类似这种..
#3
select * into 表1 from 表t where fuhao='a'
select * into 表2 from 表t where fuhao='b'
select * into 表3 from 表t where fuhao='c'
select * into 表4 from 表t where fuhao='d'
......
#4
用group by,这个根据条件是生成子表查询命令。
#5
MSSQL的都写了 MYSQL的建议去相应版问问
#6
1
#7
利用游标获取不同的fuhao,然后分别创建不同的表.
大致为:
declare @fuhao varchar(20);
declare cur cursor fast_forward for
select distinct fuhao from t;
open cur;
fetch next from cur into @fuhao;
while @@fetch_status=0
begin
exec('select * into tmp_''' + @fuhao + ''' from t where fuhao = ''' + @fuhao + '''')
fetch next from cur into @@fuhao;
end
close cur;
deallocate cur;
大致为:
declare @fuhao varchar(20);
declare cur cursor fast_forward for
select distinct fuhao from t;
open cur;
fetch next from cur into @fuhao;
while @@fetch_status=0
begin
exec('select * into tmp_''' + @fuhao + ''' from t where fuhao = ''' + @fuhao + '''')
fetch next from cur into @@fuhao;
end
close cur;
deallocate cur;
#1
建议去MYSQL版.
#2
create table tb(ID int,xkm varchar(3),fuhao varchar(1),tape varchar(2))
insert into tb
select 1,'xk1','a','1s' union all
select 2,'xk2','b','2s' union all
select 3,'xk3','a','1s' union all
select 4,'xk4','b','1s'
select 'select * into #'+ltrim(row_number() over (order by fuhao))
+' from tb where fuhao='''+fuhao+'''' from tb
group by fuhao
/*
select * into #1 from tb where fuhao='a'
select * into #2 from tb where fuhao='b'
*/
思路上类似这种..
#3
select * into 表1 from 表t where fuhao='a'
select * into 表2 from 表t where fuhao='b'
select * into 表3 from 表t where fuhao='c'
select * into 表4 from 表t where fuhao='d'
......
#4
用group by,这个根据条件是生成子表查询命令。
#5
MSSQL的都写了 MYSQL的建议去相应版问问
#6
1
#7
利用游标获取不同的fuhao,然后分别创建不同的表.
大致为:
declare @fuhao varchar(20);
declare cur cursor fast_forward for
select distinct fuhao from t;
open cur;
fetch next from cur into @fuhao;
while @@fetch_status=0
begin
exec('select * into tmp_''' + @fuhao + ''' from t where fuhao = ''' + @fuhao + '''')
fetch next from cur into @@fuhao;
end
close cur;
deallocate cur;
大致为:
declare @fuhao varchar(20);
declare cur cursor fast_forward for
select distinct fuhao from t;
open cur;
fetch next from cur into @fuhao;
while @@fetch_status=0
begin
exec('select * into tmp_''' + @fuhao + ''' from t where fuhao = ''' + @fuhao + '''')
fetch next from cur into @@fuhao;
end
close cur;
deallocate cur;