Reputation: 562
This is what I'm using now
public function nuevaPlantilla(){
$query = $this->sql->prepare("SELECT max(did) as nuevodid FROM ".self::tabla_plantillas);
$exc = $query->execute();
if (!$exc){
return false;
}
$resultado = $query->get_result();
$datos = $resultado->fetch_all();
$did = ($datos[0][0]*1)+1;
$query = $this->sql->prepare("INSERT INTO ".self::tabla_plantillas." (did, quien, tipo_usuario, did_filtro, valor, pagina) VALUES (?,?,?,?,?,?)");
if (!$query){
return false;
}
$query->bind_param("isssss", $did, $this->quien, $this->tipo, "", "", $this->qh);
$exc = $query->execute();
$query->close();
return $exc;
}
It works, but, is it possible to make the same thing with only one query?
PLEASE DO NOT SUGGEST ME TO USE AUTO_INCREMENT ID. Because more than one row would have the same did.
Upvotes: 0
Views: 279
Reputation: 204904
Use something like this:
insert into destination_table (id, col2, col3)
select * from
(
select coalesce(max(id),0)+1,
'other_value',
3
from source_table
) x
Upvotes: 1
Reputation: 334
INSERT INTO table (field, field)
SELECT field, field FROM table
WHERE field='something';
Upvotes: 0