2013-03-12 22 views
0

IM子查詢,但我不使用活動記錄,有問題即時執行這個查詢,它給了我一個錯誤編號1064MySQL查詢,使用笨作爲我的框架,笨

essentialy,即時通訊試圖插入一堆數據的一個表,而是從其他表

$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

這是不是一個完美的查詢,因爲我不知道你的數據庫的設計,但它應該儘量引導你在正確的方向如語法所示。

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子句中的那些參數即可。

+0

謝謝,讓我看看 – kastulo 2013-03-12 21:50:07

+0

你有沒有這個運氣? – 2013-03-13 14:16:48