WordPress:从每个用户记录中选择wp_users和wp_metadata中的所有关联元数据到一行

WordPress:从每个用户记录中选择wp_users和wp_metadata中的所有关联元数据到一行

问题描述:

This is how I've been able to extract all of the relavant data into a single row for each user record:

SELECT user_login, user_pass, user_email, user_registered,
meta1.meta_value AS billing_address_1,
meta2.meta_value as billing_address_2,
meta3.meta_value as billing_city,
meta4.meta_value as billing_first_name,
meta5.meta_value as billing_last_name,
meta5.meta_value as billing_postcode,
meta5.meta_value as billing_country,
meta5.meta_value as billing_state
FROM wp_users users,
wp_usermeta meta1,
wp_usermeta meta2,
wp_usermeta meta3,
wp_usermeta meta4,
wp_usermeta meta5,
wp_usermeta meta6,
wp_usermeta meta7,
wp_usermeta meta8
WHERE users.id = meta1.user_id
    AND meta1.meta_key = 'billing_address_1'
AND users.id = meta2.user_id
    AND meta2.meta_key = 'billing_address_2'
AND users.id = meta3.user_id
    AND meta3.meta_key = 'billing_city'
AND users.id = meta4.user_id
    AND meta4.meta_key = 'billing_first_name'
AND users.id = meta5.user_id
    AND meta5.meta_key = 'billing_last_name'
AND users.id = meta6.user_id
    AND meta6.meta_key = 'billing_postcode'
AND users.id = meta7.user_id
    AND meta7.meta_key = 'billing_country'
AND users.id = meta8.user_id
    AND meta8.meta_key = 'billing_state'

However, at this point (and I haven't even listed half of the meta data that needs to be extracted), phpMyAdmin is telling me that my JOIN is too big ...

Is there a more efficient way of doing this?

这就是我能够将每个的所有相关数据提取到一行中的方法 user code> record: p>

  SELECT user_login,user_pass,user_email,user_registered,
meta1.meta_value AS billing_address_1,
meta2.meta_value as billing_address_2,
meta3.meta_value as billing_city  ,
meta4.meta_value为billing_first_name,
meta5.meta_value为billing_last_name,
meta5.meta_value为billing_postcode,
meta5.meta_value为billing_country,
meta5.meta_value为billing_state 
FROM为wp_users用户,
wp_usermeta meta1,
wp_usermeta meta2,
wp_usermeta  meta3,
wp_usermeta meta4,
wp_usermeta meta5,
wp_usermeta meta6,
wp_usermeta meta7,
wp_usermeta meta8 
WHERE users.id = meta1.user_id 
 AND meta1.meta_key ='billing_address_1'
AND users.id = meta2.user_id \  n AND meta2.meta_key ='billing_address_2'
AND users.id = meta3.user_id 
 AND meta3.meta_key ='billing_city'
AND users.id = meta4.user_id 
 AND meta  4.meta_key ='billing_first_name'
AND users.id = meta5.user_id 
 AND meta5.meta_key ='billing_last_name'
AND users.id = meta6.user_id 
 AND meta6.meta_key ='billing_postcode'
AND users.id  = meta7.user_id 
 AND meta7.meta_key ='billing_country'
AND users.id = meta8.user_id 
 AND meta8.meta_key ='billing_state'
  code>  pre> 
 
 

但是,此时(我甚至没有列出需要提取的一半元数据),phpMyAdmin告诉我,我的 JOIN code>太大了...... p> \ n

有更有效的方法吗? p> div>

If you need a user with all details then you can simply use get_userdata function as given below

$user_info = get_userdata(1); // 1 is user ID here (required)
echo 'Username: ' . $user_info->user_login . "
";
echo 'User level: ' . $user_info->user_level . "
";
echo 'User ID: ' . $user_info->ID . "
";
// more...

If you want to get the details of currently logged in user then you can use get_currentuserinfo function

global $current_user;
get_currentuserinfo();

echo 'Username: ' . $current_user->user_login . "
";
echo 'User email: ' . $current_user->user_email . "
";
echo 'User first name: ' . $current_user->user_firstname . "
";
echo 'User last name: ' . $current_user->user_lastname . "
";
echo 'User display name: ' . $current_user->display_name . "
";
echo 'User ID: ' . $current_user->ID . "
";

You can use the get_user_meta() function. It will return all of the user meta information for a specific user. Call the function for each user, in a loop.

http://codex.wordpress.org/Function_Reference/get_user_meta

What about creating a view and running the select on that instead?