简体   繁体   English

使用子查询将mysql表插入另一个表

[英]Insert a mysql table into another table with sub query

I'm Importing a CSV with two columns into an existing table using PHP (I'm using Joomla/Virtuemart) 我正在使用PHP将具有两列的CSV导入到现有表中(我正在使用Joomla / Virtuemart)

The columns in the existing table are id (user id) and product_id. 现有表中的列是ID(用户ID)和product_id。

The columns from the CSV file are product_sku and username so I need to first join to the appropriate tables to match the id (user id) with the username, and proudct_sku to product_id. CSV文件中的列是product_sku和username,因此我需要首先连接到适当的表,以将id(用户id)与用户名进行匹配,而自豪ct_sku与product_id进行匹配。 I also need to trim leading zeros off the username, and whitespace from the productsku which I've done using sql. 我还需要修剪掉用户名中的前导零以及使用sql完成的productku中的空格。

I created a temporary table containing the data in the CSV, then joined onto both tables. 我创建了一个临时表,其中包含CSV中的数据,然后将它们联接到两个表上。 I've got the data I need back but now need to insert that data into the existing table. 我已经获得了需要的数据,但是现在需要将该数据插入到现有表中。 Ideally I'd like to do this with mySQL, is it possible to do this as a sub query? 理想情况下,我想使用mySQL进行此操作,是否可以将其作为子查询来进行?

Would there be a massive difference in speed? 速度会有巨大差异吗?

function do_query($file)
{

   //  $sql = "LOAD DATA INFILE ".$file['tmp_name']." INTO TABLE FavouritesTmp FIELDS TERMINATED BY ','  LINES TERMINATED BY '\n' (data1, data2)";

   $sql = "CREATE TEMPORARY TABLE FavouritesTmp (
                productsku VARCHAR(50) NOT NULL, 
                user VARCHAR(50) NOT NULL
        )";
   $this->_db->setQuery($sql);
   $this->_db->query();
   $handle = fopen($file['tmp_name'], "r");
   do
   {
      if ($data[0])
      {
         $sql = "INSERT INTO `FavouritesTmp` (productsku, user) VALUES 
                    ( 
                        '" . $data[0] . "', 
                        '" . $data[1] . "'
                    ) 
                ";
         $this->_db->setQuery($sql);
         $this->_db->query();
      }
   }

   while ($data = fgetcsv($handle, 1000, ",", "'"));
   $sql = "SELECT TRIM(LEADING  '0' FROM user) AS username, 
        productsku AS productsku, 
        jos_users.id AS id, 
        jos_vm_product.product_id AS product_id 
        FROM FavouritesTmp 
        LEFT JOIN jos_users ON (  TRIM(LEADING  '0' FROM username ) = jos_users.username )
        LEFT JOIN jos_vm_product ON  TRIM(productsku) = jos_vm_product.product_sku";
   $this->_db->setQuery($sql);
   print_r($this->_db->loadAssocList());
}

You can just use insert . . . select 您可以只使用insert . . . select insert . . . select insert . . . select syntax: insert . . . select语法:

INSERT INTO ExistingTable(username, productsku, id, product_id)
    SELECT TRIM(LEADING  '0' FROM user) AS username, 
           productsku AS productsku, 
           jos_users.id AS id, 
           jos_vm_product.product_id AS product_id 
    FROM FavouritesTmp 
    LEFT JOIN jos_users ON (  TRIM(LEADING  '0' FROM username ) = jos_users.username )
    LEFT JOIN jos_vm_product ON  TRIM(productsku) = jos_vm_product.product_sku;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM