duannai5879 2016-05-14 18:06
浏览 15
已采纳

尝试将匹配的行插入到与会话表中的列匹配的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;
    }
  • 写回答

1条回答 默认 最新

  • drasebt1835 2016-05-15 07:48
    关注

    Answer is:

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

    with PDO

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算