【问题标题】:trying to insert matching rows into an empty table from a products table that match a column in the session table尝试将匹配的行插入到与会话表中的列匹配的产品表中的空表中
【发布时间】:2016-09-10 20:11:56
【问题描述】:

我需要有关实现此 INSERT 过程的完整代码的帮助: 我有一个空的 NATAN_Procesos 表,一个包含产品 Natan_Productos 的完整表,以及一个存储比较值 Giro2_ID 的第三个表 Natan_Sesion。

$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)"); 

在查询运行且没有任何错误后,不会将任何内容加载到表中。 我错过了什么? 我应该得到一个包含大约 12 种与 Giro2_ID 匹配的产品的列表

我有以下查询可以很好地输出到屏幕,并且非常相似:

 //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>" . "\n";
    } 
} 
 $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;
    }

【问题讨论】:

  • 您是否意识到您正在尝试 fetchAll 处理不返回任何内容的查询?那是 INSERT 查询,而不是 SELECT 查询。
  • 我实际上已经尝试省略这些行,但 $stmt->execute(); 仍然没有结果; on or off or $result = ... on or off 到目前为止,没有任何组合有效。因此,我需要帮助部分。我只找到来自 INSERT 和 on 的代码数据,但没有找到 INSERT、UPDATE 等前后的变量或查询代码有什么帮助吗?
  • 我删除了 awons 有问题的代码。这仍然没有在任何地方存储任何东西。 awons,你说一个什么都不返回的查询。请详细说明。唯一有问题的部分是我原始帖子中的第一段代码。
  • 哦,我明白了,准备线?
  • 以防万一任何人都可以从中受益,解决方案是: $stmt1 = $db->query( "INSERT INTO db.destinationtable SELECT * FROM db.sourcetable WHERE columnname = (SELECT comparisoncolumnname FROM db.comparisontable)");

标签: php mysql insert conditional-statements


【解决方案1】:

答案是:

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

使用 PDO

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-27
    • 1970-01-01
    • 2015-08-13
    • 1970-01-01
    • 2021-10-21
    • 1970-01-01
    相关资源
    最近更新 更多