Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在同一查询中选择+插入_Php_Mysql_Sql - Fatal编程技术网

Php 在同一查询中选择+插入

Php 在同一查询中选择+插入,php,mysql,sql,Php,Mysql,Sql,这就是我现在用的 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->ge

这就是我现在用的

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;
}
这是可行的,但是,是否可以只使用一个查询就完成相同的操作


请不要建议我使用自动增量ID。因为不止一行具有相同的ID。

请使用类似以下内容:

INSERT INTO table (field, field)
SELECT field, field  FROM table
WHERE field='something';
insert into destination_table (id, col2, col3)
select * from 
(
  select coalesce(max(id),0)+1,
         'other_value',
         3
  from source_table
) x

从技术上讲,不可以。您不能在同时从中选择的同一个表中插入或删除。是的,它是:语法是:insert into DETINATION\u t col1,col2 select maxid,source中的“other\u values”_t@MarcB:这是可能的:它在@juergend有效,我刚刚在maxdid中添加了+1。如果桌子是空的,有没有办法把它当作1来处理?@NelsonGaldemanGraziano:是的,看看我的答案。我很好奇。结尾的x代表什么?您需要命名一个子查询。我用x作为名字。这对于语法是必要的。仅此而已。这正是我使用的查询,我不需要子查询。插入到.self::tabla_plantillas中。基恩、蒂波、乌萨里奥、菲尔特罗、瓦洛尔、帕吉纳是否选择了聚结最大值0+1、、、、、、、、、、、?,?,?,?,?,?FROM.self::tabla_plantillas