简体   繁体   English

MySQL查询,codeigniter中的子查询

[英]MySQL query, subquery in codeigniter

im using codeigniter as my framework, but i'm not using active records, im having trouble executing this query, it gives me a Error Number 1064 即时通讯使用codeigniter作为我的框架,但我没有使用活动记录,即时通讯执行此查询时遇到问题,它给我一个错误号1064

essentialy, im trying to insert a bunch of data to a table, but querying some id numbers from other tables 本质上,即时通讯试图向表中插入一堆数据,但从其他表中查询一些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;
    }  

can someone help me with this query? 有人可以帮我这个查询吗?

thanks... 谢谢...

A select statement can only have one FROM clause. 一个select语句只能有一个FROM子句。

http://dev.mysql.com/doc/refman/5.0/en/select.html http://dev.mysql.com/doc/refman/5.0/en/select.html

Look into using JOIN 调查使用JOIN

http://dev.mysql.com/doc/refman/5.0/en/join.html http://dev.mysql.com/doc/refman/5.0/en/join.html

This is not a perfect query since I do not know the design of your database, but it should steer you in the right direction as far as the syntax goes. 因为我不知道您的数据库的设计,所以这不是一个完美的查询,但是它应该将语法引导到正确的方向。

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

Essentially this will join all of the data you need into one table before you add into "productos". 本质上,这将把您需要的所有数据添加到一个表中,然后再添加到“ productos”中。 This is the proper way to do what you need with SQL. 这是使用SQL进行所需操作的正确方法。 Of course, this also depends on the relationships between the tables--in this example I assumed that you had foreign keys in table_1 that referred to data in other tables. 当然,这也取决于表之间的关系-在此示例中,我假设您在table_1中具有引用其他表中数据的外键。

You will then be able to execute this query based on whatever parameters you want/need--just refer to those in the WHERE clause. 然后,您将能够根据想要/需要的任何参数执行此查询-只需引用WHERE子句中的参数即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM