Jump to content

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


Recommended Posts

Ok...I think this is getting what I'm wanting, but I'm getting repeats.  It makes sense, but I don't want repeats since I'm eventually going to make a count of how many of each type of entry I have. 

 

The custom field shows where the User lives by a defined district.

The s2member_level shows what subscription level the User has.

I brought in the Join because it adds the user name and email address to the results.

 

SELECT * FROM wp_usermeta um1 
INNER JOIN wp_usermeta um2
JOIN wp_users u
ON um1.user_id=um2.user_id
WHERE um1.meta_key = 'wp_s2member_custom_fields' AND um2.meta_value LIKE "%s2member_level%"
AND um1.user_id = u.ID
ORDER BY um1.user_id

 

 

  • Replies 51
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

What appears to be happening is it's reflecting all the inputs, so if a User (such as my test accounts and a few others) have gone in and switched where they live.  I'm getting their old and current locations.  Is there a way to limit my search to the latest input from each ID?

Edit: I'm going to post this anyway, even if I started writing it before your last reply, and you have since indicated there is more than one value/matching rows.

 

If you were to post some sample results and indicate what part of it is duplicated, someone can probably help. We don't know what different types of data there are for any one user_id, don't have your database to test with, don't have or don't want to setup wp, and don't know what result you are getting.

 

Edit: As to your last post, of course. What value is present in the data that can be used to identify the latest value?

This would be one example: (I assume um1.meta_id could be differentiated from um2.meta_id.)

 

umeta_id,user_id,meta_key,meta_value,umeta_id,user_id,meta_key,meta_value

13068,439,wp_s2member_custom_fields,a:1:{s:6:"county";s:1:"1";},6680,439,wp_capabilities,a:1:{s:15:"s2member_level1";s:1:"1";}

13068,439,wp_s2member_custom_fields,a:1:{s:6:"county";s:1:"1";},15195,439,wp_capabilities,a:1:{s:15:"s2member_level1";s:1:"1";}

15569,439,wp_s2member_custom_fields,a:1:{s:6:"county";s:1:"0";},6680,439,wp_capabilities,a:1:{s:15:"s2member_level1";s:1:"1";}

15569,439,wp_s2member_custom_fields,a:1:{s:6:"county";s:1:"0";},15195,439,wp_capabilities,a:1:{s:15:"s2member_level1";s:1:"1";}

 

Since I don't know what the umeta_id value means, I don't think I can offer any further assistance. One could start making assumptions about it, but one could also make an assumption that wp would not store the same data in two different rows (the two sets of wp_capabilities are identical for that user, but with different umeta_id values.)

 

 

It's just the unique key for that table.  You asked me to show you the data, and I did. 

 

umeta_id | user_id | meta_key | meta_value

 

13068 | 439 | wp_s2member_custom_fields | a:1:{s:6:"county";s:1:"1";}

15195 | 439 | wp_capabilities | a:1:{s:15:"s2member_level1";s:1:"1";}

 

15569 | 439 | wp_s2member_custom_fields | a:1:{s:6:"county";s:1:"0";}

15195 | 439 | wp_capabilities | a:1:{s:15:"s2member_level1";s:1:"1";}

 

 

 

Those are the columns.  With each change a new row is shown.  The query takes each pair of rows in the table and makes in one row in the query.  I had a database issue switching from WP-multiuser to WP, and it duplicated some early users.  I eliminated that data for you.  What you see above now is the User switching what County they live in.  I only want it to reflect the most recent, based on user_id.

 

 

Don't know if anyone's said this (yeh, call me lazy, but 3 pages  :o ) but are you quoting that query with double quotes or single quotes? Because you either need to quote your query in single quotes, or quote your clause conditions in single quotes and quote the query in double quotes.

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%")

 

$query = "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%')";

 

 

Like that?

 

SELECT GROUP_CONCAT(CONCAT(umeta_id, '~', user_id, '~', meta_key, '~', meta_value) SEPARATOR '@') 
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%")
GROUP BY user_id

 

 

 

??

 

 

You could use 2 query's, otherwise, I need you to explain EXACTLY what you're expecting to get back from the query?

SELECT * FROM wp_usermeta um1 
INNER JOIN wp_usermeta um2
JOIN wp_users u
ON um1.user_id=um2.user_id
WHERE um1.meta_key = 'wp_s2member_custom_fields' AND um2.meta_value LIKE "%s2member_level%"
AND um1.user_id = u.ID
ORDER BY um1.user_id

 

 

 

Is that the one?

 

 

SELECT * FROM wp_usermeta um1 
INNER JOIN wp_usermeta um2
JOIN wp_users u
ON um1.user_id=um2.user_id
WHERE um1.meta_key = 'wp_s2member_custom_fields' AND um2.meta_value LIKE "%s2member_level%"
AND um1.user_id = u.ID
GROUP BY um1.user_id
ORDER BY um1.user_id, um1.umeta_id DESC

 

//edit sorry group by before order by

At this point, because the point of storing serialized data, is to store an array in a database, I suspect that there should only be one row with the wp_s2member_custom_fields per user_id and when you edit your profile, it reads that row, modifies the values, and writes the result back into that row.

Any chance that the non-zero 'country' value is the 'active' one? (The current version of that plugin apparently stores a two character country code, not a number.)

 

The current version of that plugin actually calls a wp function to get the data. I doubt it expects more than one matching row. Someone (I don't have the time right now) might want to trace through the code for that plugin and the relevant function in wp to find out if it does anything special when a request for user information matches more than one row.

I just installed the latest of wp and that s2member script and when you create a field in the profile and update the value (tested multiple times, different values, then back to a zero), it updates the one single row in the database. The row is initially created on the first edit/update of the value.

 

It would appear that your sets of two rows for wp_s2member_custom_fields with the same user_id are due to multiple data insertion in your database and are not the result of the operation of the script.

 

Edit: I would make the user_id/meta_key columns a composite and unique index to prevent duplicates.

Slightly off topic, but now that I am somewhat up to speed on what that data means, if you were doing this query for security purposes, you would want the suggested AND um2.meta_key = 'wp_capabilities' in the query so that someone could not just store "administrator" or a higher user level string as a value in any one of their meta_value fields and magically become an administrator or gain a higher user level.

OK...I tested it, and that appears to be the case now, which is good.  It doesn't appear that has always been the case.  I have only one custom field, and my test accounts clearly prompted separate rows of data when I changed its County.  The four most recent accounts don't have duplicates.

 

It could have been a change via an update in the last month.

 

 

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.