2012-04-11 11 views
1

Ich bin in der Lage, eine Zeile in meiner Tabelle mit dem folgenden Code zu klonen:Clont MySQL Reihe plus neue eindeutige ID

$uuid = 'blahblah';  
INSERT INTO events (title, subtitle) SELECT title, subtitle FROM events WHERE uuid = '$uuid' 

Allerdings habe ich eine neue eindeutige ID für die neu geklonte Zeile generieren möge. Der Name dieser Tabellenspalte lautet "uuid". Wie lege ich eine neue eindeutige ID während des Klonens ein? Ich habe eine Auto-Index-Spalte vom Typ INT, aber ich habe auch eine eindeutige ID-Spalte vom Typ VARCHAR für jede Zeile (gemischt alphanumerisch).

Vielen Dank im Voraus.

Antwort

-1

Ich bin ein Oracle-Typ, nehmen, so dass advisement unter, aber ich es so etwas tun würde: hier

INSERT INTO events (id, title, subtitle) 
SELECT nextval('sequence_name'), title, subtitle 
    FROM events 
WHERE uuid = '$uuid' 
+0

MySQL hat keine NEXTVAL-Funktion. –

0
/* 
eduardoaf.com 
https://github.com/eacevedof/ 
*/ 
DELIMITER $$ 

DROP PROCEDURE IF EXISTS `prc_clone_row`$$ 

CREATE PROCEDURE `prc_clone_row`(
    sTableName VARCHAR(25) 
    ,sId VARCHAR(5) 
    ) 
BEGIN 
    SET @sSQL := CONCAT('SELECT (MAX(id)+1) AS idnew FROM ',sTableName,' INTO @sIdNew'); 
    PREPARE sExecute FROM @sSQL; 
    EXECUTE sExecute; 

    IF (@sIdNew IS NOT NULL) THEN 
     SET @sSQL := CONCAT('CREATE TEMPORARY TABLE tempo_table SELECT * FROM ',sTableName,' WHERE id = ',sId,'; '); 
     PREPARE sExecute FROM @sSQL; 
     EXECUTE sExecute; 

     SET @sSQL := CONCAT('UPDATE tempo_table SET id=',@sIdNew,' WHERE id=',sId,'; '); 
     PREPARE sExecute FROM @sSQL; 
     EXECUTE sExecute;   

     SET @sSQL := CONCAT('INSERT INTO ',sTableName,' SELECT * FROM tempo_table WHERE id=',@sIdNew,'; '); 
     PREPARE sExecute FROM @sSQL; 
     EXECUTE sExecute; 

     SET @sSQL := CONCAT('SELECT * FROM ',sTableName,' ORDER BY id DESC;'); 
     PREPARE sExecute FROM @sSQL; 
     EXECUTE sExecute; 
    ELSE 
     SELECT CONCAT('TABLE ',sTableName,' IS EMPTY!!!') AS msg; 
    END IF; 

END$$ 

DELIMITER ; 

CALL prc_clone_row('app_cms_content','1'); 
+0

Neben Code sollte Ihre Antwort wahrscheinlich eine Erklärung (?) Enthalten ... – poplitea