Forum Moderators: open

Message Too Old, No Replies

Mysql UPDATE where [field] like '[field]'

         

jake66

5:25 am on Dec 25, 2007 (gmt 0)

10+ Year Member



Is this possible?

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.

lammert

1:43 am on Dec 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes, LIKE can be used in a WHERE clause.

But the easiest way is to create a test table, add two or three rows to it and run a test with a query tool or the command line interface of your SQL database to see if the result is what you intended to do.

jake66

5:19 am on Jan 5, 2008 (gmt 0)

10+ Year Member



The LIKE doesn't seem to be working.

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?

ZydoSEO

6:09 am on Jan 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The reason your LIKE statement is not working is that it is meant to be used with wildcard characters. See:

[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]

jake66

11:33 am on Jan 7, 2008 (gmt 0)

10+ Year Member



Thank you, that worked flawlessly!

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

(and null without the quotes)

ZydoSEO

4:14 pm on Jan 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In SQL when you are referring to/testing for NULL values in a SELECT use IS NULL (you cannot check for NULL using '=').

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]

jake66

3:48 am on Jan 9, 2008 (gmt 0)

10+ Year Member



Thank you ZydoSEO, I will bookmark this topic for future use.. but before I read your reply I noticed today, that the table was numerical, so I had to use ='0' in order to execute my query.

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. :)

ZydoSEO

3:32 pm on Jan 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Varchar and numeric fields can have NULL values (unless the field is defined with a default value). I think of NULL as meaning it doesn't have a set value at the moment. So you can use IS NULL on both alpha (varchar/char) and numeric fields.