19

我想从 wp_users 和 wp_usermeta 表创建一个视图,以便我可以从外部应用程序查询视图中的行。基本身份验证详细信息存储在 wp_users 中(例如,用户名、密码、电子邮件、id),其他字段存储在 wp_usermeta 表中的键/值对中。

wp_users 表的结构如下:

id | login | password | email
-----------------------------
1  | bsmith| abc123   | b@foo.com
2  | jjones| def456   | k@bah.com

wp_usermeta 表的结构如下:

id | user_id | meta_key | meta_value
------------------------------------
1  | 1       | firstname| bob
2  | 1       | lastname | smith
3  | 1       | country  | denmark
4  | 2       | firstname| jan
5  | 2       | lastname | jones
6  | 2       | country  | germany

我想以 MYSQL VIEW 的形式得到这样的数据:

id | login | password | email    | firstname | lastname | country
-----------------------------------------------------------------
1  | bsmith| abc123   | b@foo.com| bob       | smith    | denmark
2  | jjones| def456   | k@bah.com| jan       | jones    | germany

我知道我至少需要在 wp_users 和 wp_usermeta 之间进行内部连接,但是我还需要在 wp_usermeta 中执行子查询以将这些行连接在一起。

我用于获取用户行的查询当然非常简单:

select u1.id, u1.login, u1.password, u1.email from wp_users u1

我将元数据作为一行获取的查询是:

select m1.user_id, m1.meta_value as firstname, m2.meta_value as lastname, m3.meta_value as country
from wp_usermeta m1
join wp_usermeta m2 on (m1.user_id = m2.user_id and m2.meta_key = 'last_name')
join wp_usermeta m3 on (m2.user_id = m3.user_id and m3.meta_key = 'country')
where m1.meta_key = 'first_name'

那么如何将这两个查询连接在一起 - u1 的数据和 u1.id = m1.userid 所在的行?

4

3 回答 3

37

据我所知,您的做法是正确的,只需将它们放在一起:

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
于 2012-05-24T04:30:44.100 回答
15

我发现 Toote 的解决方案有效,但在 MySQL 数据库上执行“select * from users_with_meta_view”需要大约 17 秒的性能。

我用这样的视图将其缩短到 0.0054 秒:

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
于 2017-06-22T18:09:51.647 回答
1
$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 } ?>
于 2017-05-23T13:02:02.713 回答