这就是我现在用的
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;
}
它可以工作,但是,是否可以只用一个查询来完成同样的事情?
请不要建议我使用 AUTO_INCRMENT ID。因为不止一行会有相同的内容。
最佳答案
使用这样的东西:
insert into destination_table (id, col2, col3)
select * from
(
select coalesce(max(id),0)+1,
'other_value',
3
from source_table
) x
SQLFiddle demo
关于php - 在同一查询中选择+插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19795264/