现在有2张表,旧表oldtable,新表newtable,我要把旧表中数据全导入新表然后删除旧表,可就在导时出现数据重复导oldtable 数据有:id   userid   n1   y1   n2   y2   school
1      a     1990  09  1993  06    初中
2      a     1993  09  1996  06    高中
3      b     2003  09  2007  06    高中最终新表应该有的效果为:id   userid      all 
1      a       1、1990年09月---1993年06月   初中
               2、1993年09月---1996年06月   高中2      b       2003年09月---2007年06月   高中--------------------------------------------------------------------
可实际上我插入数据库的数据有重复哪位高人能帮我看看,错在哪,谢谢,还有个问题就是使用chr(10).chr(13)在数据库能看见换行,可显示在td中是无换行的~~~
<?
mysql_connect("localhost","root","");
mysql_select_db("test");
$get1=mysql_query("select userid from oldtable;");
$num=mysql_num_rows($get1);
for($i=1;$i<=$num;$i++){
$get3=mysql_fetch_array($get1);
$get2=mysql_query("select userid,n1,y1,n2,y2,school from oldtable where userid='$get3[userid]';"); 
$n=mysql_num_rows($get2);
if($n!=1){ //如果旧表中 userid相同的不止一条就执行
for($s=1;$s<=$n;$s++){
$g2=mysql_fetch_array($get2);
$a.=$s."、".$g2[n1]."年".$g2[y1]."月"."---".$g2[n2]."年".$g2[y2]."月".chr(32).$g2[school].chr(10).chr(13);
}
}else{
$g2=mysql_fetch_array($get2);
$a=$g2[n1]."年".$g2[sy1]."月"."---".$g2[n2]."年".$g2[y2]."月".chr(32).$g2[school];
}
mysql_query("update newtable set all='$a' where userid='$g2[userid]';");
}
?>

解决方案 »

  1.   

    1.你的这句
    $get1=mysql_query("select userid from oldtable;");
    改为
    $get1=mysql_query("select distinct userid from oldtable;");
    即可------
    2. 你这个目的不用写php, 直接一句sql就可以解决: 
    大概如下:
    insert into newtable 
    select userid, group_cat(n1,'N',y1,'Y---',n2,'Y',y2,'Y', school SEPARATOR "\n" )
    from oldtable 
    group by userid---------
    3. html里用<br>换行, \r\n都只显示为一点空白
    php里用nl2br转换一下即可
      

  2.   

    chr(10).chr(13)
    在数据库里是
    id  userid      all 
    1      a      1、1990年09月---1993年06月  初中 
                    2、1993年09月---1996年06月  高中 
    这效果,但直接
    $get3=mysql_fetch_array($get1); 
    print $get3[all]
    出来的效果就是:
    id  userid      all 
    1      a      1、1990年09月---1993年06月  初中 2、1993年09月---1996年06月  高中 怎么在读出来转了?
    通常批量替换是在数据库中直接换的多,用php进行导的少?
      

  3.   


    ===>
     print nl2br($get3['all']);
    通常批量替换是在数据库中直接换的多,用php进行导的少?
    ============
    这个.....如果不怕麻烦,用php也行, 不过你觉得那个更方便更快呢?
      

  4.   

    group_cat(n1,'N',y1,'Y---',n2,'Y',y2,'Y', school SEPARATOR "\n" ) 这一句似乎有问题吧~~~GROUP_CONCAT,还有里面的n1,'N',y1,'Y---'都是什么意思了
      

  5.   


    用方法1:不行,要的是2个相同的合并为一条,不是只要一条用方法2:报错ERROR 1136 (21S01): Column count doesn't match value count at row 1
      

  6.   

    抱歉我搞错了,除了第2条helloyou0 写的有点问题,其它几点回答都对,谢谢
      

  7.   

    加上字段名即可
    insert into newtable(userid, all) 
    select userid, group_cat(n1,'N',y1,'Y---',n2,'Y',y2,'Y', school SEPARATOR "\n" ) 
    from oldtable 
    group by userid