简体   繁体   English

PHP MySQL使用where子句INSERT来自另一个表的数据,并将唯一值插入到相同的行中

[英]PHP MySQL INSERT data from another table using the where clause and insert unique values to the same rows

I want to insert data from one table into another where one field equals another in both tables. 我想将一个表中的数据插入到另一个表中,其中一个字段等于两个表中的另一个字段。 So far this works. 到目前为止这个工作。 The problem is, I also need to insert additional data into those same rows that is not included on the first table. 问题是,我还需要将其他数据插入到第一个表中未包含的相同行中。


//enter rows into database 
foreach($_POST['sku'] as $row=>$sku)
{ 
//this is the data that needs to be added to the table 
$item_sku=$sku;
$image="/$item_sku.jpg";
$small_image="/$item_sku.jpg"; 
$thumbnail="/$item_sku.jpg";

//currently this is what is working to import data from one table to the other
$sql= "INSERT INTO magento_import (sku, description, price) 
       SELECT PR_SKU, PR_Description, PR_UnitPrice
       FROM products 
       WHERE PR_SKU = '$sku'";

//I need something here to add the above variables to the same row where PR_SKU = '$sku'

if (!mysql_query($sql))
{
die('Error: '.mysql_error()); 
}
echo "$row record added";
}

The columns for the missing data on magento_table are called 'image', 'small_image', and 'thumbnail'. magento_table上缺少数据的列称为“image”,“small_image”和“thumbnail”。 This is simple a hack to put data from an old product table into a new product table, export as a CSV, and run a profile in Magento. 将旧产品表中的数据放入新产品表,导出为CSV并在Magento中运行配置文件,这很简单。 I don't need to worry about SQL injections. 我不需要担心SQL注入。 It's something I'm running off of a local machine. 这是我在本地机器上运行的东西。 I'm trying to avoid as much manual data entry as possible while switching products over to a new ecommerce system. 在将产品切换到新的电子商务系统时,我试图避免尽可能多的手动数据输入。 Thanks for any help you can give. 谢谢你提供的所有帮助。

Selecting literal values should do what you intend: 选择文字值应该按照您的意图执行:

$sql= "INSERT INTO magento_import (sku, description, price, image, small_image, thumbnail) 
       SELECT PR_SKU, PR_Description, PR_UnitPrice, \"$image\", \"$small_image\", \"$thumbnail\"
       FROM products 
       WHERE PR_SKU = '$sku'";

You can use another update statement to do this. 您可以使用另一个更新语句来执行此操作。 I doubt if you can do this with one single query 我怀疑你是否可以用一个查询来做到这一点

foreach($_POST['sku'] as $row=>$sku)
{ 
//this is the data that needs to be added to the table 
$item_sku=$sku;
$image="/$item_sku.jpg";
$small_image="/$item_sku.jpg"; 
$thumbnail="/$item_sku.jpg";

//currently this is what is working to import data from one table to the other
$sql= "INSERT INTO magento_import (sku, description, price) 
SELECT PR_SKU, PR_Description, PR_UnitPrice
FROM products 
WHERE PR_SKU = '$sku'";

//I need something here to add the above variables to the same row where PR_SKU = '$sku'

if (!mysql_query($sql))
{
die('Error: '.mysql_error()); 
}else {

$sql = "UPDATE magento_import SET item='$item',image='$image',small_image='$small_image',thumbnail='$thumbnail' WHERE PR_SKU = '$sku'";
echo "$row record added";
}

}

You can just add the variables directly to the query. 您只需将变量直接添加到查询中即可。 As long as they are quoted they will work as simple values. 只要他们被引用,他们就会像简单的价值观一样工作。

$sql= "INSERT INTO magento_import (sku, description, price, x, y, z) 
   SELECT PR_SKU, PR_Description, PR_UnitPrice, '$x' AS x, '$y' AS y, '$z' AS z
   FROM products 
   WHERE PR_SKU = '$sku'";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在mysql中使用php插入动态输入值将一张表同时复制到另一张表 - How to insert dynamic input values copy one table to another table at the same time in mysql using php PHP / MYSQL从一个表中插入多行,其中一个条件使用TRANSACTIONS - PHP/MYSQL INSERT multiple rows FROM a TABLE WHERE one criteria using TRANSACTIONS MySQL将数据从一个表插入到另一个选定的行 - Mysql insert data from one table to another selected rows 如何使用php将来自mysql的选定数据插入mysql中的另一个表? - How to insert selected data from mysql with php to another table in mysql? PHP:从表单和同一表中的另一个表插入数据 - PHP: Insert data from form and another table in the same table PHP MySQL将数据从一个表插入到另一个表 - PHP MySQL insert data from one table to another table MYSQL在不同的表PHP中插入相同的值 - MYSQL Insert the same values in different table PHP 使用LAST_INSERT_ID()在PHP / MySQL中进行两次插入,并从另一个表中进行行 - Two inserts in PHP/MySQL using LAST_INSERT_ID() and rows from another table 在使用 php 同时按 id 在 mysql 中删除之前,在另一个表中插入旧数据 - insert old data on another table before deleting in mysql using php at the same time by id 使用where子句mysql将数据从一个表传输到另一个表 - Transferring data from one table to another using a where clause mysql
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM