Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/264.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.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 MySQL查询,codeigniter中的子查询_Php_Mysql_Codeigniter - Fatal编程技术网

Php MySQL查询,codeigniter中的子查询

Php MySQL查询,codeigniter中的子查询,php,mysql,codeigniter,Php,Mysql,Codeigniter,我使用codeigniter作为我的框架,但我没有使用活动记录,我在执行这个查询时遇到问题,它给了我一个错误号1064 基本上,我试图向一个表中插入一组数据,但从其他表中查询一些id号 $titulo = $datos['titulo']; $tipo = $datos['tipo']; $autor = $datos['autor']; $autor2 = $datos['autor2']; $editorial = $datos['ed

我使用codeigniter作为我的框架,但我没有使用活动记录,我在执行这个查询时遇到问题,它给了我一个错误号1064

基本上,我试图向一个表中插入一组数据,但从其他表中查询一些id号

$titulo = $datos['titulo'];
    $tipo   =   $datos['tipo'];
    $autor  =   $datos['autor'];
    $autor2 =   $datos['autor2'];
    $editorial =    $datos['editorial'];
    $ano        =   $datos['ano'];
    $paginas    =   $datos['paginas'];
    $descripcion =  $datos['descripcion'];
    $image_path =   'hola';
    $genero     =   $datos['genero'];
    $genero2    =   $datos['genero2'];


    $sql = "INSERT INTO productos (titulo, autor_id, autor2_id, editorial_id, ano, paginas, genero_id,
             genero2_id, tipo, descripcion, image_path)
             SELECT ? AS titulo,
             id FROM autores WHERE nombre_autor=?,
             id FROM autores WHERE nombre_autor=?,
             id FROM editoriales WHERE nombre_editorial=?,
             ? as ano, 
             ? as paginas,
             id FROM generos WHERE nombre_genero=?,
             id FROM generos WHERE nombre_genero=?,
             ? as tipo,
             ? as descripcion,
             ? as image_path";

    if($this->db->query($sql, array($titulo, $autor, $autor2, $editorial, $ano, $paginas, $genero, $genero2, $tipo, $descripcion, $image_path))){
        return true;
    }else{
        return false;
    }  
有人能帮我解答这个问题吗


谢谢…

select语句只能有一个FROM子句

研究如何使用JOIN


这不是一个完美的查询,因为我不知道数据库的设计,但它应该能引导您在语法方面朝着正确的方向前进

INSERT INTO productos (titulo, autor_id, autor2_id, editorial_id, ano, paginas, genero_id,
         genero2_id, tipo, descripcion, image_path)
SELECT a.titulo, b.id, b.id2, c.id, a.ano, a.paginas, d.id, d.id2, a.tipo, a.description, a.image_path
FROM table_1 a
JOIN table_2 b ON a.autor_id = b.id
JOIN table_3 c ON a.editorial_id = c.id
JOIN table_4 d ON a.genero_id = d.id
WHERE a.id = 25
本质上,这将在您添加到“productos”之前将您需要的所有数据连接到一个表中。这是使用SQL执行所需操作的正确方法。当然,这也取决于表之间的关系——在本例中,我假设表_1中有外键,它引用了其他表中的数据


然后,您将能够基于您想要/需要的任何参数执行此查询——只需参考WHERE子句中的参数即可。

谢谢,让我检查一下。您在这方面有什么进展吗?