Table a 
-------------
id name cid
1   a1   1
2   a2   1
3   a3   1
4   a4   1
-------------Table b
-------------
tid id tit
1   1   b1
2   1   b2
3   1   b3
4   2   b4
5   2   b5
6   3   b6
7   3   b7
8   4   b8a,b两个表id关联如果想把表a,b中的数据批量复制一下,且新关联也变更成为如下:Table a 
-------------
id name cid
1   a1   1
2   a2   1
3   a3   1
4   a4   1
5   a1   2
6   a2   2
7   a3   2
8   a4   2-------------Table b
-------------
tid id tit
1   1   b1
2   1   b2
3   1   b3
4   2   b4
5   2   b5
6   3   b6
7   3   b7
8   4   b8
9   5   b1
10   5   b2
11   5   b3
12   6   b4
13   6   b5
14   7   b6
15   7   b7
16   8   b8请问这样的sql语句该怎么写?60分求教。感谢。

解决方案 »

  1.   

    本帖最后由 ACMAIN_CHM 于 2011-03-06 20:05:12 编辑
      

  2.   

    Table b
    -------------
    tid id tit
    1   1   b1
    2   1   b2
    3   1   b3
    4   2   b4
    5   2   b5
    6   3   b6
    7   3   b7
    8   4   b8
    9   5   b1
    10   5   b2
    11   5   b3
    12   6   b4
    13   6   b5
    14   7   b6
    15   7   b7
    16   8   b8
    insert into b (id,tit) select id+(select max(id) from b),tit from b
      

  3.   

    没看出A,B表有什么关联。 (不要高估你的汉语表达能力或者我的汉语理解能力)
       建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
       参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
       
       1. 你的 create table xxx .. 语句
       2. 你的 insert into xxx ... 语句
       3. 结果是什么样,(并给以简单的算法描述)
       4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
       
       这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。   
      

  4.   

    嗯,好的。问题重新描述:
    create table a
    (
     id int(20),
     name varchar(20),
     cid int(10)
    )
    insert into a select 'a1','1'
    insert into a select 'a2','1'
    insert into a select 'a3','1'
    insert into a select 'a4','1'Table a  
    -------------
    id name cid
    1 a1 1
    2 a2 1
    3 a3 1
    4 a4 1
    -------------create table b(
     tid int(30),
     id int(20),
     tit varchar(20)
    )insert into a select '1','b1'
    insert into a select '1','b2'
    insert into a select '1','b3'
    insert into a select '2','b4'
    insert into a select '2','b5'
    insert into a select '3','b6'
    insert into a select '3','b7'
    insert into a select '4','b8'Table b
    -------------
    tid id tit
    1 1 b1
    2 1 b2
    3 1 b3
    4 2 b4
    5 2 b5
    6 3 b6
    7 3 b7
    8 4 b8Table a,b中id是关联的
    ===================================================================
    如何通过批量导入cid=2的数据,实现表a为(cid=1和cid=2的数据相同):Table a  
    -------------
    id name cid
    1 a1 1
    2 a2 1
    3 a3 1
    4 a4 1
    5 a1 2
    6 a2 2
    7 a3 2
    8 a4 2与此同时,表b中也对关联cid=1的数据进行复制,如下:
    Table b
    -------------
    tid id tit
    1 1 b1
    2 1 b2
    3 1 b3
    4 2 b4
    5 2 b5
    6 3 b6
    7 3 b7
    8 4 b8
    9 5 b1
    10 5 b2
    11 5 b3
    12 6 b4
    13 6 b5
    14 7 b6
    15 7 b7
    16 8 b8table b数据从第九行开始,对应的id=5,为表a中新增的数据,如何匹配?谢谢