› 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 · Jul 18, 2012 · 5080 views
    This topic created in 5184 days ago, the information mentioned may be changed or developed.
    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 replies  •  1970-01-01 08:00:00 +08:00
    mudone
        1
    mudone  
       Jul 18, 2012   ❤️ 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
       Jul 18, 2012
    @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;
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   847 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 64ms · UTC 19:52 · PVG 03:52 · LAX 12:52 · JFK 15:52
    ♥ Do have faith in what you're doing.