一条记录由于某个字段值可能取多个,每条记录只显示这个字段的一个值,所以一条记录显示多行如:
id name columnid(多值字段) 
1  aa    2001
2  aa    2002
3  aa    2003
现在想实现:根据name查询出所有的columnid与前台得到的几个值如(2002,2003)比较
如何进行比较,并把columnid中多余的删除,少的插入?????

解决方案 »

  1.   

    先建一个临时针表表2(结构同表1,id自增),把前台得到的值导进该表后
    删除多余的:
    delete from 表1 where id in 
      ( select a.id from 表1 a where not exists(select 1 from 表2 b where a.name=b.name 
        and a.colunmid=b.colunmid)插入少的:
     insert into 表1(name,columnid)
     select a.name,a.columnid from  表2 a where not exists(select 1 from 表1 b where a.name=b.name 
        and a.colunmid=b.colunmid)
      

  2.   


    循环开始取前台第一个name删除多余的:将前台得到各name对应的值循环从数组中取出并组成字符串str:"'前台值1','前台值2'...'前台值n'delete from 表1 where name='当前名字' and colunmid not in (ss) --需要做拼接插入少的:
     子循环:
     数组中取出第一个值colunmid1 if not exists(select 1 from  表2 a where name='当前名字' and colunmid=colunmid1)
     begin
     insert into 表1(name,columnid1)
     end
     取下一个colunmid并循环
     子循环结束取下一个name并循环循环结束