关于mysql数据库表的插入修改问题

关于mysql数据库表的插入修改问题
我这有个数据表
name offset
dao[1]\ch[1] 1
dao[1]\ch[2] 3
dao[2]\ch[1] 5
dao[2]\ch[2] 7
dao[3]\ch[1] 9
dao[3]\ch[2] 11
我想在每个满足条件select %\ch[1]后面都加入dao[*]\ch[1.5] 相应的offset也要改动
结果是这样的:
name offset
dao[1]\ch[1] 1
dao[1]\ch[1.5] 3
dao[1]\ch[2] 5
dao[2]\ch[1] 7
dao[2]\ch[1.5] 9
dao[2]\ch[2] 11
dao[3]\ch[1] 13
dao[3]\ch[1.5] 15
dao[3]\ch[2] 17
求实现代码,有好的方法,有加分
一不小心点了三次提交,有好的答案小子我给就240分!大侠们多多帮忙啊,急.
想到一个解决方案,有不对的请提:
select * from table where name like dao%\ch% //所有道都提出来
//int count=1;
res=mysql_store_result(conn);
for(int i=0;i{
row=mysql_fetch_row(res);
char * strname=row[0]; int offset=row[1];
char * namelast=拆分(strname);//字符串拆分后方便比较
if(strcmp(namelast,"ch[1]")==0)//如果符合条件
{
int shuzhi = offset+2;
insert into table values(name,shuzhi) where //在满足条件的行后插入
//count++;
}
else
{
row[1]=offset+2*2;
}
}
create table t_result
(
name varchar(30),
offset int
)
go

insert into t_result
select 'dao[1]\ch[1]',1 union
select 'dao[1]\ch[2]',3 union
select 'dao[2]\ch[1]',5 union
select 'dao[2]\ch[2]',7 union
select 'dao[3]\ch[1]',9 union
select 'dao[3]\ch[2]',11
go

--进入临时表处理

select * into #t_tmp from t_result where charindex('ch[1]',name)>1

--处理字模碰符亏液串
update #t_tmp set name=replace(name,'ch[1]','ch[1.5]')

--处销码物理offset和插入
declare @currOff int
while(exists(select top 1 1 from #t_tmp)) --只要还有记录
begin
set @currOff=(select top 1 offset from #t_tmp order by offset asc)
update t_result set offset=offset+2 where offset>@currOff
update #t_tmp set offset=offset+2 where offset>@currOff
insert into t_result select (select name from #t_tmp where offset=@currOff),@currOff+2
delete #t_tmp where offset=@currOff
end
go

drop table #t_tmp

select * from t_result order by offset
高手啊。。。等好答案 自己也学习一下。
这个肯定得用程序了吧。
光sql命令 不太好整 。
哪来这么变态的需求的。
← 上一篇: 下一篇: →