Duplicate record with new ID and another custom field

0

I have a database ( basedatos ) and a table ( contratos ) with the following fields:

Id
Nombre
Fecha  (timestamp)
Vendedor
Telefono
Gestionado

I want to insert in a php page a button that duplicates the record but with the consecutive Id, the date is timestamp and in the nombre field I concatenate the value that already has with R . That is, if the field nombre is originally "Pedro", now the new record will be "Pedro R ".

I tried:

INSERT INTO TABLA1 (CAMPO1, CAMPO2) 
  SELECT CAMPO1, CAMPO2
  FROM DUAL
  WHERE CAMPO=VALOR

but I do not see how it concatenates.

    
asked by Pedro Jimenez 09.06.2017 в 10:13
source

3 answers

6

The query you have is a good starting point.

The problem is that you do not find how to concatenate "R" to the result. For this you can use CONCAT() (in English) as follows:

INSERT INTO 'contratos' (nombre, id)
  SELECT
     CONCAT(nombre, ' R'), id + 1
  FROM
     contratos
  WHERE
     nombre = "Pedro"

Notice that we are doing INSERT INTO 'nombre_de_tabla' (campos, a, insertar) because we only insert two.

Note that this answer addresses the problem at the MySQL level. At the PHP level we should do other things, but for that we need to see where you are.

    
answered by 09.06.2017 / 11:06
source
1

The answer given by @fedorqui is correct, as long as the id is not auto-incremental and when adding 1 to the current id you could generate a duplicated key with another record.

INSERT INTO 'contratos' (nombre)
  SELECT
     CONCAT(nombre, ' R')
  FROM
     contatos
  WHERE
     id = ?
    
answered by 09.06.2017 в 17:54
0

I think that only by adding the ID field and in the select the NULL value should already increase the ID:

 INSERT INTO TABLA1 (ID,CAMPO1, CAMPO2) 
  SELECT null,CAMPO1, CAMPO2
  FROM DUAL
  WHERE CAMPO=VALOR
    
answered by 10.06.2017 в 19:20