尝试将匹配的行插入到与会话表中的列匹配的products表中的空表中

问题描述:

I need help with the full code to implement this INSERT process: I have empty NATAN_Procesos Table, a full table with products Natan_Productos, and a third table Natan_Sesion that stores a comparative value Giro2_ID.

$stmt1 = $db->query("INSERT INTO Natan_Procesos (
 Producto_ID,
 Producto_Nombre,
 Producto_Descripcion,
 Proveedor_ID,
 Sucursal_ID,
 Categoria_ID,
 Giro2_ID,
 Producto_Precio,
 Producto_Descuento,
 Producto_Imagen,
 Producto_Prioridad
 )
 VALUES(( SELECT Giro2_ID FROM Natan_Productos  
 WHERE Giro2_ID = giro2 FROM Natan_Sesion)"); 

Nothing is loaded to the table after the query runs without any errors. What am I missing? I should be getting a list of about 12 products that match Giro2_ID

I have the following query that outputs to screen just fine and its quite similar:

 //build the table
 echo "<table style='border: solid 1px black;'>";
echo "<tr><th>uid</th><th>monto</th><th>personas</th><th>ciudad</th><th>giro1</th><th>giro2</th><th>ip</th><th>Presupuesto_Individual</th></tr>";

class TableRows9 extends RecursiveIteratorIterator { 
    function __construct($it) { 
        parent::__construct($it, self::LEAVES_ONLY); 
    }

    function current() {
        return "<td style='width:150px;border:1px solid black;'>" . parent::current(). "</td>";
    }

    function beginChildren() { 
        echo "<tr>"; 
    } 

    function endChildren() { 
        echo "</tr>" . "
";
    } 
} 
 $stmt = $db->prepare("SELECT * FROM Natan_Productos WHERE Giro2_ID = (SELECT giro2 FROM Natan_Sesion )"); 
    $stmt->execute();

    // set the resulting array to associative
    $result = $stmt->setFetchMode(PDO::FETCH_ASSOC); 
    foreach(new TableRows9(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) { 
        echo $v;
    }

Answer is:

$stmt1 = $db->query( "INSERT INTO db.destinationtable SELECT * FROM db.sourcetable WHERE columnname = (SELECT comparisoncolumnname FROM db.comparisontable)");

with PDO