I want to create a view from both the wp_users and wp_usermeta tables so that I can query rows in the view from an external application. Basic auth details are stored in wp_user
As far as I know, you are doing it the right way and just need to put them all together:
SELECT
u1.id,
u1.login,
u1.password,
u1.email,
m1.meta_value AS firstname,
m2.meta_value AS lastname,
m3.meta_value AS country
FROM wp_users u1
JOIN wp_usermeta m1 ON (m1.user_id = u1.id AND m1.meta_key = 'first_name')
JOIN wp_usermeta m2 ON (m2.user_id = u1.id AND m2.meta_key = 'last_name')
JOIN wp_usermeta m3 ON (m3.user_id = u1.id AND m3.meta_key = 'country')
WHERE
-- CONDITIONS ON the user you want to select based any field
I found that Toote's solution works but is a performance hog on a MySQL database taking about 17 seconds to execute a "select * from users_with_meta_view".
I got this down to 0.0054 sec with a view structured like this instead:
CREATE OR REPLACE VIEW users_with_meta_view AS
SELECT
u.id,
u.user_login AS login,
u.user_pass AS password,
u.user_email AS email,
(select meta_value from wp_usermeta where user_id = u.id and meta_key = 'first_name' limit 1) as first_name,
(select meta_value from wp_usermeta where user_id = u.id and meta_key = 'last_name' limit 1) as last_name,
(select meta_value from wp_usermeta where user_id = u.id and meta_key = 'country' limit 1) as country
FROM wp_users u
$sql= "SELECT
u1.parent_id,
m1.meta_value AS headline,
m2.meta_value AS profilephoto
FROM wp_vandor u1
JOIN wp_usermeta m1 ON (m1.user_id = u1.child_id AND m1.meta_key = 'headline')
JOIN wp_usermeta m2 ON (m2.user_id = u1.child_id AND m2.meta_key = 'profilephoto')
WHERE m1.user_id != $currentuser_id";
$classifieds = $wpdb->get_results($sql);
foreach ($classifieds as $classified) { ?>
<p><?php echo $classified->profilephoto; ?></p>
<h3><?php echo $classified->headline; ?></h3>
<?php } ?>