我使用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子句
http://dev.mysql.com/doc/refman/5.0/en/select.html考虑使用JOIN
http://dev.mysql.com/doc/refman/5.0/en/join.html这不是一个完美的查询,因为我不知道您的数据库的设计,但它应该引导您在正确的方向就语法而言。
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完成所需操作的正确方法。当然,这也取决于表之间的关系——在本例中,我假设在table_1中有引用其他表中的数据的外键。
然后,您将能够根据您想要/需要的任何参数执行此查询—只需引用WHERE子句中的参数。