我使用codeigniter作为我的框架,但我没有使用活动记录,我无法执行此查询,它给我一个错误号1064
essentialy,我试图将一堆数据插入表中,但是从其他表中查询一些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;
}
有人可以帮我解决这个问题吗?
感谢...
答案 0 :(得分:0)
答案 1 :(得分:0)
这不是一个完美的查询,因为我不知道你的数据库的设计,但它应该引导你在语法的正确方向。
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子句中的参数即可。