close ad
Help us test the new Databridge BETA with MySQLi support
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

I need to update all prices and dimensions with a batch process

Thread began 3/14/2012 7:32 am by mrs | Last modified 3/16/2012 8:05 am by mrs | 909 views | 3 replies |

mrs

I need to update all prices and dimensions with a batch process

Hi, I have been trying to get MySQL to update the prices of all products within the ps4_products table by batch processing it with a query, but I can't get it working. I know it's possible, but I am just doing something wrong and need help.

Here's how am I trying to do it. I have the ps4_products table and I have created a table called _import_products which has all of the up-to-date info in it. I am then trying to ask MySQL to merge the info of the two tables by matching the SKU. I am expecting each record to have updated prices, weight, dimensions etc, but what I am getting is a new series of records in addition to the existing records.

Here's my query:

SELECT ProductSku, COUNT(ProductSku) _import_products FROM _import_products
GROUP BY ProductSku;

UPDATE ps4_products
INNER JOIN _import_products ON (_import_products.ProductSku = ps4_products.ProductSKU)
SET ps4_products.ProductPrice = _import_products.ProductPrice;
SET ps4_products.ProductWeight = _import_products.ProductWeight;
SET ps4_products.ProductWidth = _import_products.ProductWidth;
SET ps4_products.ProductHeight = _import_products.ProductHeight;
SET ps4_products.ProductLength = _import_products.ProductLength;



Here's the structure of the tables:

DROP TABLE IF EXISTS `_import_products`;

CREATE TABLE `_import_products` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`ProductSKU` varchar(50) DEFAULT NULL,
`ProductPrice` float(12,2) DEFAULT NULL,
`ProductWeight` float(12,2) DEFAULT NULL,
`ProductWidth` decimal(12,4) DEFAULT NULL,
`ProductHeight` decimal(12,4) DEFAULT NULL,
`ProductLength` decimal(12,4) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2684 DEFAULT CHARSET=latin1;


DROP TABLE IF EXISTS `ps4_products`;

CREATE TABLE `ps4_products` (
`ProductID` int(12) NOT NULL AUTO_INCREMENT COMMENT 'None',
`ProductSKU` varchar(50) DEFAULT NULL COMMENT 'Product Code/SKU',
`ProductBrand` varchar(255) DEFAULT NULL COMMENT 'Brand Name',
`ProductName` text NOT NULL COMMENT 'Product Description',
`ProductPrice` float(12,2) NOT NULL DEFAULT '0.01',
`ProductRetailPrice` float(12,2) DEFAULT NULL,
`ProductShipping` float(12,2) DEFAULT NULL,
`ProductWeight` float(12,2) DEFAULT '0.00',
`ProductWidth` decimal(12,4) DEFAULT NULL,
`ProductHeight` decimal(12,4) DEFAULT NULL,
`ProductLength` decimal(12,4) DEFAULT NULL,
`ProductCartDesc` text COMMENT 'Product Description',
`ProductShortDesc` text COMMENT 'Product Description',
`ProductLongDesc` text COMMENT 'Product Description',
`ProductThumb` varchar(100) NOT NULL DEFAULT '' COMMENT 'Product Code/SKU',
`ProductImage` varchar(100) NOT NULL DEFAULT '' COMMENT 'Product Code/SKU',
`ProductUpdateDate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`ProductStartDate` datetime DEFAULT NULL,
`ProductEndDate` datetime DEFAULT NULL,
`ProductStock` float(12,2) DEFAULT '0.00',
`ProductLive` tinyint(1) DEFAULT '1',
`ProductUnlimited` tinyint(1) DEFAULT '1',
`ProductDigital` varchar(250) DEFAULT NULL,
`ProductMaxDownloads` int(12) DEFAULT NULL,
`ProductNonTaxable` int(1) DEFAULT '0',
`ProductTitle` text,
`ProductMetaKeywords` varchar(255) DEFAULT NULL,
`ProductMetaDescription` varchar(255) DEFAULT NULL,
`ProductCategories` text COMMENT 'Category',
PRIMARY KEY (`ProductID`)
) ENGINE=MyISAM AUTO_INCREMENT=6220 DEFAULT CHARSET=utf8;

As you can see, I've added a few column to the table where sizes are kept.




I'd appreciate your help with this.

Thanks.

Mat

Sign in to reply to this post

Jason ByrnesWebAssist

the update statement should look more like this:


UPDATE ps4_products
SET ps4_products.ProductPrice = _import_products.ProductPrice, ps4_products.ProductWeight = _import_products.ProductWeight, ps4_products.ProductWidth = _import_products.ProductWidth, ps4_products.ProductHeight = _import_products.ProductHeight, ps4_products.ProductLength = _import_products.ProductLength
FROM ps4_products, _import_products
WHERE _import_products.ProductSku = ps4_products.ProductSKU



see this page for an example:
update-data-in-one-table-with-data-from.html

Sign in to reply to this post

mrs

Cheers Jason. I had this version working before I received your response, so I managed to get it working. I was in a proper mess beforehand though.

UPDATE ps4_products
LEFT JOIN _import_products ON (_import_products.ProductSku = ps4_products.ProductSKU)
SET ps4_products.ProductPrice = _import_products.ProductPrice, ps4_products.ProductWeight = _import_products.ProductWeight, ps4_products.ProductWidth = _import_products.ProductWidth, ps4_products.ProductHeight = _import_products.ProductHeight, ps4_products.ProductLength = _import_products.ProductLength;

The only thing is, if I don't have a product price available in the import table for a product that exists in the product table, the price for that existing product is overwritten to 0.00 which is dangerous to say the least. How can I stop this from happening?

Thanks.

Sign in to reply to this post

mrs

Don't worry, I've just twigged that I hadn't got the WHERE clause in place.

If any body is interested in this capability, then here is the process to update products in your products table by batch processing from something like a CSV file.

1. Create an import table. The code for my import table is:

DROP TABLE IF EXISTS `_import_products`;

CREATE TABLE `_import_products` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`ProductSKU` varchar(50) DEFAULT NULL,
`ProductPrice` float(12,2) DEFAULT NULL,
`ProductWeight` float(12,2) DEFAULT NULL,
`ProductWidth` decimal(12,4) DEFAULT NULL,
`ProductHeight` decimal(12,4) DEFAULT NULL,
`ProductLength` decimal(12,4) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2684 DEFAULT CHARSET=latin1;


2. Import the updated prices to the import table (I use Sequel Pro to handle imports etc).


3. Merge the two tables with this query:

UPDATE ps4_products
LEFT JOIN _import_products ON (_import_products.ProductSku = ps4_products.ProductSKU)
SET ps4_products.ProductPrice = _import_products.ProductPrice, ps4_products.ProductWeight = _import_products.ProductWeight, ps4_products.ProductWidth = _import_products.ProductWidth, ps4_products.ProductHeight = _import_products.ProductHeight, ps4_products.ProductLength = _import_products.ProductLength
WHERE _import_products.ProductSku = ps4_products.ProductSKU;


Obviously, this works for my import table, and I have added fields to the products table to allow for product dimensions. It will need to be adjusted to suit your requirement.

HTH

Mat

Sign in to reply to this post

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...