php - Insert a mysql table into another table with sub query -
i'm importing csv 2 columns existing table using php (i'm using joomla/virtuemart)
the columns in existing table id (user id) , product_id.
the columns csv file product_sku , username need first join appropriate tables match id (user id) username, , proudct_sku product_id. need trim leading zeros off username, , whitespace productsku i've done using sql.
i created temporary table containing data in csv, joined onto both tables. i've got data need need insert data existing table. ideally i'd mysql, possible sub query?
would there massive difference in speed?
function do_query($file) { // $sql = "load data infile ".$file['tmp_name']." table favouritestmp fields terminated ',' lines terminated '\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"); { if ($data[0]) { $sql = "insert `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' user) username, productsku productsku, jos_users.id id, jos_vm_product.product_id product_id favouritestmp left join jos_users on ( trim(leading '0' 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 use insert . . . select
syntax:
insert existingtable(username, productsku, id, product_id) select trim(leading '0' user) username, productsku productsku, jos_users.id id, jos_vm_product.product_id product_id favouritestmp left join jos_users on ( trim(leading '0' username ) = jos_users.username ) left join jos_vm_product on trim(productsku) = jos_vm_product.product_sku;
Comments
Post a Comment