Jump to content

HELP - Using UPDATE with SELECT and CONCAT


doyoonk

Recommended Posts

In WordPress there's a table called wp_usermeta which stores some of user information... which has following structure:

 

umeta_id    user_id    meta_key      meta_value

--------------------------------------------------------

5000          100          first_name    John

5001          100          last_name      Doe

5002          100          nickname      John Doe

5003          101          first_name    Tom

5004          101          last_name    McDonald

5005          101          nickname     

--------------------------------------------------------

 

What I'm trying to do is, for users that have blank 'meta_value' for the 'meta_key = nickname', I'm trying to UPDATE the record by using CONCAT of 'first_name' and 'last_name'.

 

I've got to the point where I can SELECT the CONCATednated value by using the following SQL statement but I can't seem to use this to update 'meta_value' for the 'meta_key = nickname':

SELECT CONCAT(a.meta_value, ' ', b.meta_value)

FROM wp_usermeta as a,wp_usermeta as b WHERE a.meta_key='first_name' AND b.meta_key='last_name' AND a.user_id = b.user_id

 

Please let me know if you have any idea how to achieve this.

 

Thanks in advance!

Link to comment
Share on other sites

try

UPDATE wp_usermeta a
INNER JOIN wp_usermeta b ON a.user_id = b.user_id AND b.meta_key = 'first_name'
INNER JOIN wp_usermeta c ON a.user_id = c.user_id AND c.meta_key = 'last_name'
SET a.meta_value = CONCAT(b.meta_value, ' ', c.meta_value)
WHERE a.meta_key = 'nickname' AND a.meta_value IS NULL;

Link to comment
Share on other sites

Thanks for the reply!

 

I just tried but there were 0 rows affected.  I checked the records but the previously blank nickname fields did not have the "first_name last_name" saved, even though first_name/last_name do exist for those particular users.

 

Is there something I should modify to get it working?

 

Thank you very much!

Link to comment
Share on other sites

try

UPDATE wp_usermeta a
INNER JOIN wp_usermeta b ON a.user_id = b.user_id AND b.meta_key = 'first_name'
INNER JOIN wp_usermeta c ON a.user_id = c.user_id AND c.meta_key = 'last_name'
SET a.meta_value = CONCAT(b.meta_value, ' ', c.meta_value)
WHERE a.meta_key = 'nickname' AND a.meta_value IS NULL;

 

I just tried changing "IS NULL" to " = '' " and it worked perfectly!  Thank you very much for your help! :D

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.