skip to Main Content

I want to select values ‘b’ and ‘c’ by meta key and user id = 197

select issue

Select cc.meta_value, fname,cc.meta_value lname FROM usermeta cc where 
user_id = 197 and (cc.meta_key = 'last_name' or cc.meta_key = 'first_name')

3

Answers


  1. If You want to select them in one query:

    select meta_value from YOURTABLENAME where user_id=197 
    and meta_key in ('first_name','last_name')
    

    Or if You want to select them separately:

    select meta_value from YOURTABLENAME where user_id=197 and meta_key='last_name';
    select meta_value from YOURTABLENAME where user_id=197 and meta_key='first_name';
    
    Login or Signup to reply.
  2. Try this:

    In will get you all the values in the specified list

    Select cc.meta_value fname,cc.meta_value lname 
    FROM usermeta cc where 
    user_id = 197 and cc.meta_key in('last_name','first_name')
    
    Login or Signup to reply.
  3. You could use the second condition in the WHERE clause to achieve what you want. It’s important to wrap the second condition inside parentheses. Like this:

    SELECT * 
    FROM wp_usermeta 
    WHERE user_id = 1 
    AND 
    (
    CONVERT(meta_key USING utf8) = 'first_name' 
    OR
    CONVERT(meta_key USING utf8) = 'last_name'
    )
    

    Which outputs this:

    enter image description here


    If you only need the meta_value column, then you could use this:

    SELECT meta_value 
    FROM wp_usermeta 
    WHERE user_id = 1 
    AND 
    (
    CONVERT(meta_key USING utf8) = 'first_name' 
    OR
    CONVERT(meta_key USING utf8) = 'last_name'
    )
    

    Which outputs this:

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search