数据库版本 MySQL 5.1.41
有2个表,现要将表一的id和sid字段插入到表二(对应tid和sid)
表一的pid和color字段如果重复的情况下,则插入的tid为最小的那条纪录的id(sid不变)
表的结构如下CREATE TABLE `t1`(
  `id` int(1) unsigned NOT NULLL AUTO_INCREMENT,
  `pid` mediumint(1) unsigned NOT NULL,
  `sid` int(1) unsigned NOT NULL,
  `color` char(20) CHARCTER SET gbk NOT NULL,
  PRIMARY KEY (`id`),
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;CREATE TABLE `t2`(
  `tid` int(1) unsigned NOT NULL,
  `sid` int(1) unsigned NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
测试数据
INSERT INTO `t1`(`id`,`pid`,`sid`,`color`) VALUES
(1,1,1,'abc'),
(2,1,1,'ijk'),
(3,1,1,'xyz'),
(4,1,2,'abc'),/*与id=1重复*/
(5,1,2,'qwer'),
(6,1,3,'qwer'),/*与id=5重复*/
(7,1,3,'ijk'),/*与id=2重复*/
(8,2,4,'abc'),
(9,2,4,'xyz'),
(10,2,5,'abc'),/*与id=8重复*/
(11,2,5,'ijk'),
(12,2,5,'def'),
(13,2,5,'iop'),
(14,2,6,'asd'),
(15,2,6,'fgh');
期望结果
SELECT * FROM `t2`;
+-----+-----+
| tid | sid |
+-----+-----+
| 1   | 1   |
| 2   | 1   |
| 3   | 1   |
| 1   | 2   |
| 5   | 2   |
| 5   | 3   |
| 2   | 3   |
| 8   | 4   |
| 9   | 4   |
| 8   | 5   |
| 11  | 5   |
| 12  | 5   |
| 13  | 5   |
| 14  | 6   |
| 15  | 6   |
+-----+-----+

解决方案 »

  1.   

    参考下贴中的多种方法 (N=1)http://topic.csdn.net/u/20091231/16/2f268740-391e-40f2-a15e-f243b2c925ab.html
    [征集]分组取最大N条记录方法征集,及散分....
      

  2.   

    楼主请给测试用例的时候自己试一下,否则是浪费所有想帮你的人的时间。mysql> CREATE TABLE `t1`(
        ->   `id` int(1) unsigned NOT NULLL AUTO_INCREMENT,
        ->   `pid` mediumint(1) unsigned NOT NULL,
        ->   `sid` int(1) unsigned NOT NULL,
        ->   `color` char(20) CHARCTER SET gbk NOT NULL,
        ->   PRIMARY KEY (`id`)
        -> );
    ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
    corresponds to your MySQL server version for the right syntax to use near 'NULLL
     AUTO_INCREMENT,
      `pid` mediumint(1) unsigned NOT NULL,
      `sid` int(1) uns' at line 2
    mysql>
      

  3.   

    mysql> select * from t1;
    +----+-----+-----+-------+
    | id | pid | sid | color |
    +----+-----+-----+-------+
    |  1 |   1 |   1 | abc   |
    |  2 |   1 |   1 | ijk   |
    |  3 |   1 |   1 | xyz   |
    |  4 |   1 |   2 | abc   |
    |  5 |   1 |   2 | qwer  |
    |  6 |   1 |   3 | qwer  |
    |  7 |   1 |   3 | ijk   |
    |  8 |   2 |   4 | abc   |
    |  9 |   2 |   4 | xyz   |
    | 10 |   2 |   5 | abc   |
    | 11 |   2 |   5 | ijk   |
    | 12 |   2 |   5 | def   |
    | 13 |   2 |   5 | iop   |
    | 14 |   2 |   6 | asd   |
    | 15 |   2 |   6 | fgh   |
    +----+-----+-----+-------+
    15 rows in set (0.03 sec)mysql> insert into t2
        -> select (Select min(id) from t1 where pid=a.pid and color=a.color),sid From t1 a;
    Query OK, 15 rows affected (0.01 sec)
    Records: 15  Duplicates: 0  Warnings: 0mysql> select * from t2;
    +-----+-----+
    | tid | sid |
    +-----+-----+
    |   1 |   1 |
    |   2 |   1 |
    |   3 |   1 |
    |   1 |   2 |
    |   5 |   2 |
    |   5 |   3 |
    |   2 |   3 |
    |   8 |   4 |
    |   9 |   4 |
    |   8 |   5 |
    |  11 |   5 |
    |  12 |   5 |
    |  13 |   5 |
    |  14 |   6 |
    |  15 |   6 |
    +-----+-----+
    15 rows in set (0.00 sec)mysql>