Jump to content

Query matching multiple columns in multiple rows??? (serialized data?)


Recommended Posts

Not sure how to go about this, but I haven't been able to get the right query or even know where to look it up.  Below are two rows of data.  The following query (someone from here helped me with that query) only gives me the bottom of the two rows:

 

SELECT * 
FROM wp_usermeta 
WHERE meta_key = "wp_s2member_custom_fields" 
AND user_id IN (SELECT user_id FROM wp_usermeta WHERE meta_value LIKE "%s2member_level%")

 

umeta_id,user_id,meta_key,meta_value

16484,1117,wp_capabilities,a:1:{s:15:"s2member_level2";s:1:"1";}

16491,1117,wp_s2member_custom_fields,a:1:{s:6:"county";s:1:"2";}

 

 

How do I get both lines in my query to show up so I can use them?

  • Replies 51
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

You have meta_key equals "wp_s2member_custom_fields" (your first condition) in the last line only but not in both lines! Why do you surprised?

 

PS. scootstah is quite right when say "You should be splitting that data into separate table...". I'd add to it "...or into separate fields in one table".

You have meta_key equals "wp_s2member_custom_fields" (your first condition) in the last line only but not in both lines! Why do you surprised?

 

PS. scootstah is quite right when say "You should be splitting that data into separate table...". I'd add to it "...or into separate fields in one table".

 

I didn't say I was surprised.  I asked how do I go about querying that data. 

 

 

 

 

@Scootstah, I've never really had too many problems with it beyond just lacking a deeper knowledge of how to query a database.  It's a learning process. 

I didn't say I was surprised.  I asked how do I go about querying that data. 

It seems that you surprised :) Let's say you go to the cafe and say "give me a tea with sugar". You get a tea with sugar... But you ask "why do you put in sugar???". Correct answer - it's because you ask about it.

 

If you need both rows you have to exclude the first condition.

OK... Try this

 

SELECT * 
FROM wp_usermeta 
WHERE (meta_key = 'wp_s2member_custom_fields' or meta_key = 'wp_capabilities')
AND user_id IN (SELECT user_id FROM wp_usermeta WHERE meta_value LIKE '%s2member_level%')

 

Yes, it's true... It selects all rows where (meta_key = 'wp_s2member_custom_fields' or meta_key = 'wp_capabilities') - do you understand this condition??? AND in the same time user_id is contained in this selection "(SELECT user_id FROM wp_usermeta WHERE meta_value LIKE '%s2member_level%')".

 

It seems that you don't like to think... You even can't say what do you like to get. OK, I hope you will start to think and find a solution for you task. Now it seems you don't know what do you like :) I can't help such a person. Sorry, but I leave this topic.

SELECT * 
FROM wp_usermeta 
WHERE meta_key = "wp_s2member_custom_fields" 
AND user_id IN (SELECT user_id FROM wp_usermeta WHERE meta_value LIKE "%s2member_level%")

umeta_id,user_id,meta_key,meta_value
16484,1117,wp_capabilities,a:1:{s:15:"s2member_level2";s:1:"1";}
16491,1117,wp_s2member_custom_fields,a:1:{s:6:"county";s:1:"2";}

 

please explain again in plain English what exactly do you have and what exactly do you need.

 

to me looks like that you are asking for record that have meta_key = "wp_s2member_custom_fields" OR meta_value LIKE "%s2member_level%", but I'm just guessing... because could also be that you need

meta_key = "wp_s2member_custom_fields" AND meta_value LIKE "%s2member_level%"

"AND" produced zero results.

"OR" produced 201. 

 

It should be closer to 19 or so.  They have to be of the same user_id.  One row is noting what subscription level they have.  The other tells what county they live in.

TRY the query in reply #12. It will give you the pairs of rows you state you want.

 

However, you probably want to do a (self) JOIN to basically get one row per each user_id with all the information in it from the pairs of rows that have a s2member_level in the wp_capabilities row. This (using a join) would result in less logic because with two rows per user_id (what you are attempting now) you will need to test which of the two rows for each user_id you are dealing with as you loop through the result set. You will also need an order by term in the query so that the two rows having the same user_id are together in the result set.

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.