2012-05-24 49 views
14

我想從wp_users和wp_usermeta表創建一個視圖,以便我可以從外部應用程序查詢視圖中的行。基本身份驗證詳細信息存儲在wp_users(例如用戶名,密碼,電子郵件,ID)中,其他字段存儲在wp_usermeta表中的鍵/值對中。Wordpress用戶和usermeta--將一個表中的多行連接到另一個表中的一行

的wp_users表的結構是這樣的:

id | login | password | email 
----------------------------- 
1 | bsmith| abc123 | [email protected] 
2 | jjones| def456 | [email protected] 

的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視圖像這樣的數據來結束:

id | login | password | email | firstname | lastname | country 
----------------------------------------------------------------- 
1 | bsmith| abc123 | [email protected]| bob  | smith | denmark 
2 | jjones| def456 | [email protected]| 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的行。

+0

你是基於一些meta_value屬性中搜索用戶的計劃?如果沒有,你可以將屬性存儲在單個序列化的meta_value字段中(json編碼,或者php序列化) – Ben

+0

@Ben:nope,外部應用程序將連接到交互式查詢數據 – frumbert

回答

4

我發現Toote的解決方案作品,但在MySQL的性能豬數據庫大約需要17秒來執行「從users_with_meta_view中選擇*」。

我以期得到這個下降到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 
29

據我所知,你正在做正確的方式,只是需要把它們放在一起:

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 
+0

gar,你當然是我看到了複雜的解決方案!歡呼聲 – frumbert

+2

這非常有幫助。對於其他用戶來說,如果你在wordpress(我不認爲是OP)的時候這樣做的話,你會想用wp_usermeta和wp_users分別替換$ wpdb-> usermeta和$ wpdb->用戶。只是後人的供參考。非常有用的答案。 – Jake

+0

這真的爲我節省了大量的時間! – Scorpius

-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> 
} 
相關問題