tags:

views:

117

answers:

2

What am I doing wrong with this query? (Friday afternoon brain freeze...)

In WordPress, I used a MySQL query to make an empty custom field called "description" in all posts of a test database, and now I want to add the value "test" to that field. (This is all in the process of teaching myself how to write more complex queries.)

But I can't get this query to deal with the fact that the field has no value. 'NULL' doesn't work (and it appears that or IS NULL is what I should be using, according to other stackoverflow answers), and '%' doesn't.

UPDATE `wp_postmeta` SET `meta_value` = replace(meta_value, 'IS NULL', 'test') WHERE `meta_key` LIKE 'description'
+2  A: 

I think you want

UPDATE `wp_postmeta` SET `meta_value` = 'test'
 WHERE `meta_key` LIKE 'description'
   and `meta_value` is null
Oscar Chan
Nice - that works great. Thanks! You must have beaten Noah by only a few seconds....
songdogtech
yes, I did. I found out his post was 20 seconds after mine when I was double checking my answer. I guess I got lucky :)
Oscar Chan
+1  A: 

You could try:

UPDATE `wp_postmeta` 
SET `meta_value` = 'test'
WHERE `meta_key` LIKE 'description'
AND `meta_value` IS NULL
Noah
Thanks, as well, but the second meta_value needs back ticks....
songdogtech
HA, yeah, I see it now
Noah