Forum Moderators: open
UPDATE products
SET products_manu = Manufacturer1
WHERE products_id LIKE Man1
Tables from the database are as follows:
Product Manufacturer (WANT to set: Manufacturer1)
Products ID Man1-ProductName
Basically I want to use a search string containing a partial entry from ALL of the Products ID table and set the new field (products_manu) accordingly.
The first half of every table will be the same.
Example: man1-
The second half (after the dash) will always be different.
The LIKE only works if I use the exact model name for:
WHERE products_manu LIKE 'man1-' ORDER BY products_id asc limit 1
Is there possibly some different wordage I need to be using?
[dev.mysql.com...]
The statement:
WHERE products_id LIKE 'man1-'
is logically equivalent to
WHERE products_id = 'man1-'
because there are no wildcards. If you want to reference all instances where products_id begins with 'man1-' and are followed by anything else then use:
WHERE products_id LIKE 'man1-%'
In the above LIKE clause, the '%' is a wildcard that can represent any character or set of trailing characters. The '_' character is a wildcard that can be used to represent a single character.
So I think you need:
UPDATE products
SET products_manu = 'Manufacturer1'
WHERE products_id LIKE 'Man1-%'
Hope that helps.
[edited by: ZydoSEO at 6:14 am (utc) on Jan. 5, 2008]
I also tried updating a table with NULL as default in similar fashion, but it isn't saving.
Result:
Affected rows: 0 (Query took 0.0153 sec)
Code:
UPDATE products
SET products_manu = '21'
WHERE products_manu = '' ORDER BY products_id asc limit 1
Also tried:
UPDATE products
SET products_manu = '21'
WHERE products_manu = 'NULL' ORDER BY products_id asc limit 1
Try
UPDATE products
SET products_manu = '21'
WHERE products_manu IS NULL
Also, with most DBs ORDER BY is not valid with UPDATE/INSERT/DELETE (only SELECT). But I'm not sure about MySQL (mainly Oracle/SQL*Server experience).
[edited by: ZydoSEO at 4:15 pm (utc) on Jan. 8, 2008]
But I suppose it's safe to continue using NULL instead of default after reading your message, now I know how to update tables if I need to if they're varchar. :)