V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
? MySQL 5.5 Community Server
? MySQL 5.6 Community Server
? Percona Configuration Wizard
? XtraBackup 搭建主从复制
Great Sites on MySQL
? Percona
? MySQL Performance Blog
? Severalnines
推荐管理工具
? Sequel Pro
? phpMyAdmin
推荐书目
? MySQL Cookbook
MySQL 相关项目
? MariaDB
? Drizzle
参考文档
? http://mysql-python.sourceforge.net/MySQLdb.html
august
V2EX  ?  MySQL

mysql 怎样 clone 一条纪录?

  •  
  •   august · 2012-07-18 11:54:25 +08:00 · 3821 次点击
    这是一个创建于 4317 天前的主题,其中的信息可能已经有所发展或是发生改变。
    table 中有近百个 field ,其中 `ref_id`,`block_no`和`pile_no`是primary key, `ref_id` AUTO_INCREMENT.

    新 clone 的纪录应该是`ref_id` +1 而 `block_no` 和 `pile_no` 不变

    用 insert into `table` select * from `table` order by `ref_id` desc limit 1 很正常地失败了。

    请教高手们,怎么在不将那近百个 field 名打一次的情况下 clone 一条只有 `ref_id` + 1的新纪录?

    先谢了!
    2 条回复  ?  1970-01-01 08:00:00 +08:00
    mudone
        1
    mudone  
       2012-07-18 12:19:54 +08:00   ?? 1
    create TEMPORARY table tmptable like table;
    insert into tmptable select * from table order by id desc limit 1;
    alter table tmptable drop id;
    insert into table select '',tmptable.* from tmptable;

    备注:仅仅为了解决clone,没有考虑其他因素,酌情使用。
    august
        2
    august  
    OP
       2012-07-18 12:46:41 +08:00
    @mudone 感谢你的回复。

    发贴后没多久,我在 http://www.av8n.com/computer/htm/clone-sql-record.htm 里找到个方法,跟你的差不多,都是先建个临时表。再次感谢。

    CREATE TEMPORARY TABLE chan2 ENGINE=MEMORY SELECT * FROM channel WHERE chanid=21051;
    UPDATE chan2 SET chanid=21109; ## Change the unique key
    ## Update anything else that needs to be updated.
    INSERT INTO channel SELECT * FROM chan2;
    DROP TABLE chan2;
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1363 人在线   最高记录 6547   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 23:27 · PVG 07:27 · LAX 16:27 · JFK 19:27
    Developed with CodeLauncher
    ? Do have faith in what you're doing.


    http://www.vxiaotou.com