Forums / General Questions / SQL to change "master_categories_id" value based on "products_name"

SQL to change "master_categories_id" value based on "products_name"

Views: 877

Results 1 to 4 of 4
3 Apr 2013, 2:14 PM
#1
waynestephens avatar

waynestephens

New Zenner

Join Date:
Aug 2009
Posts:
48
Plugin Contributions:
0

SQL to change "master_categories_id" value based on "products_name"

I'd like to be able to use SQL code to search for products containing a certain word in the title (products_name) and then reset the products master_categories_id of those products to a specific number, and only if the products_quantity is zero.

I know nothing about SQL, but I've done some Googling and come up with this:

UPDATE products
SET master_categories_id = '277'
WHERE products_quantity = '0' and products_name = 'Archimede';

So in this example I would hope that all products that have the word "Archimede" in the title and have a quantity of zero, will have their master category id set to "277".

Of course, it doesn't work as the "products_name" field is in the "products_description" table, not the "products" table, and after spending a long time searching, I can't figure out how to correct this. Please help! :smile:

20th Century Glass - Antique & Collectable Glass Store

5 Apr 2013, 3:50 AM
#2
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,515
Plugin Contributions:
176

Re: SQL to change "master_categories_id" value based on "products_name"

It's a little more complicated when multiple tables are involved. To do that, must use multiple table names and add criteria to properly join the two tables on a common piece of information, etc.
Plus, you wrote products_name = 'Archimede' when you wanted it to 'affect products that have the word "Archimede" in the title', but your use of "=" checks for an exact match. Instead, you'd need to use a matching syntax, where '%' is the matching rule (much like a '*' would be used in other systems).

So, something like this would work better:

**BUT I STRONGLY RECOMMEND YOU DO NOT DO THIS!
**

update products p, products_description pd 
SET master_categories_id = 277
WHERE p.products_id = pd.products_id
AND products_quantity = 0 
AND products_name LIKE  '%Archimede%';

NOTE: THIS IS NOT NECESSARILY A COMPLETE HANDLING OF master categories though!!!!!
There's more to it than just these pieces.
Especially with v1.3.8a which is missing some safeties to recover from incorrectly mangled data.

ALWAYS DO A COMPLETE DATABASE BACKUP BEFORE RUNNING ANY SQL STATEMENTS AGAINST IT!!!!
ALWAYS DO A COMPLETE DATABASE BACKUP BEFORE RUNNING ANY SQL STATEMENTS AGAINST IT!!!!
ALWAYS DO A COMPLETE DATABASE BACKUP BEFORE RUNNING ANY SQL STATEMENTS AGAINST IT!!!!
ALWAYS DO A COMPLETE DATABASE BACKUP BEFORE RUNNING ANY SQL STATEMENTS AGAINST IT!!!!

oh, and in case I didn't emphasize it enough ... remember to ALWAYS DO A COMPLETE DATABASE BACKUP BEFORE RUNNING ANY SQL STATEMENTS AGAINST IT!!!!

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

5 Apr 2013, 4:18 AM
#3
ajeh avatar

ajeh

Oba-san

Join Date:
Sep 2003
Location:
Ohio
Posts:
62,757
Plugin Contributions:
1

Re: SQL to change "master_categories_id" value based on "products_name"

NOTE: if the Product is not already in the Category for the master_categories_id you need to fix the products_to_categories table ... both to remove/add to that table as needed ...

Linda McGrath
If you have to think ... you haven't been zenned ...

**Did YOU buy the Zen Cart Team a cup of coffee and a donut today? Just click here to support the Zen Cart Team!!**

Are you using the latest? Perhaps you've a problem that's fixed in the latest version: [Upgrade today!]
Officially PayPal-Certified! Just click here

Try our Zen Cart Recommended Services - Hosting, Payment and more ...
Signup for our Announcements Forums to stay up to date on important changes and updates!

5 Apr 2013, 4:21 AM
#4
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,515
Plugin Contributions:
176

Re: SQL to change "master_categories_id" value based on "products_name"

Agreed.

There's a lot more to altering categories than just randomly flipping the number in one table.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.