2013-10-17 35 views
0

我花了大部分時間試圖弄清楚這一點,所以我想是時候問專家了: 我試圖訪問WordPress以外的WordPress數據庫, m編寫查詢以從有點複雜的WordPress數據庫中獲取輸出。
數據庫中有幾個表使用meta_values和meta_keys將關於帖子或用戶的數據存儲在多行中。
例如,下面四個表,我需要從拉:將表加入自己和彼此

表:wp_postmeta

post_id | meta_key  | meta_value 
---------+-----------------+------------ 
2001  | _customer_user | 3 
2001  |order_description| smith 
2001  | _order_total | 300 

表:wp_posts

ID | post_type | post_status 
----+-----------+------------ 
2001| shop_order| publish 

表:wp_term_relationships

object_id | term_taxonomy_id 
----------+----------------- 
2001  | 190 

表: wp_usermeta

user_id | meta_key  |meta_value 
---------+-----------------+---------- 
    3  | first_name  | Andy 
    3  | last_name  | Mccormick 

我有了這個查詢工作的偉大,顯示訂單號(POST_ID)客戶ID,訂購介紹,和訂單總額在單行:在另一個領域我想

SELECT wp_posts.ID, wp_posts.post_title,ot.meta_value as total,od.meta_value as order_desc, cu.meta_value as customer     
     FROM wp_posts 
     LEFT JOIN wp_postmeta AS ot ON (wp_posts.ID = ot.post_id AND ot.meta_key='_order_total') 
     LEFT JOIN wp_postmeta AS od ON (wp_posts.ID = od.post_id AND od.meta_key='order_description') 
     LEFT JOIN wp_postmeta AS cu ON (wp_posts.ID =cu.post_id AND cu.meta_key='_customer_user') 
     LEFT JOIN wp_term_relationships ON wp_posts.ID = wp_term_relationships.object_id 
     WHERE wp_posts.post_type = 'shop_order' 
     AND wp_posts.post_status = 'publish' 
     AND wp_term_relationships.term_taxonomy_id = '190' 
     GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC" 

然而按名稱列出的客戶,而不是僅僅通過ID,所以我試圖從wp_usermeta表像這樣得到他們的第一個和最後一個名字:

SELECT wp_posts.ID, cu.meta_value as customer, fn.meta_vale as fname, ln.meta_value as lname     
       FROM wp_posts 
       LEFT JOIN wp_postmeta AS cu ON (wp_posts.ID = cu.post_id AND cu.meta_key='_customer_user') 
       LEFT JOIN wp_usermeta AS fn ON (cu.meta_key = fn.user_id AND fn.meta_key='first_name') 
       LEFT JOIN wp_usermeta AS ln ON (cu.meta_key = ln.user_id AND ln.meta_key='last_name') 
       LEFT JOIN wp_term_relationships ON wp_posts.ID = wp_term_relationships.object_id 
       WHERE wp_posts.post_type = 'shop_order' 
       AND wp_posts.post_status = 'publish' 
       AND wp_term_relationships.term_taxonomy_id = '190' 
       GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC; 

我也試過: LEFT JOIN wp_usermeta AS FN ON ((cu.me ta_key ='_ customer_user')= fn.user_id AND fn.meta_key ='first_name')以及大量的其他子查詢等等。

很明顯,我現在在SQL查詢中高於我的工資等級,所以任何幫助都會很棒。謝謝!

+1

從第二個查詢中得到的結果是什麼? – TheWolf

+0

#1054 - '字段列表'中的未知列'fn.meta_vale' –

回答

1

我不知道,不過你可以試試這個:

SELECT wp_posts.ID, cu.meta_value as customer, fn.meta_vale as fname, ln.meta_value as lname     
       FROM wp_posts 
       LEFT JOIN wp_postmeta AS cu ON (wp_posts.ID = cu.post_id AND cu.meta_key='_customer_user') 
       LEFT JOIN wp_usermeta AS fn ON (cu.meta_value = fn.user_id AND fn.meta_key='first_name') // Note meta_value instead of meta_key 
       LEFT JOIN wp_usermeta AS ln ON (cu.meta_value = ln.user_id AND ln.meta_key='last_name') // Note meta_value instead of meta_key 
       LEFT JOIN wp_term_relationships ON wp_posts.ID = wp_term_relationships.object_id 
       WHERE wp_posts.post_type = 'shop_order' 
       AND wp_posts.post_status = 'publish' 
       AND wp_term_relationships.term_taxonomy_id = '190' 
       GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC; 

從我想通了,cu.meta_value是在你的榜樣3,因爲它是用戶的ID,而cu.meta_key(如查詢)是'_customer_id',因此您寧願要匹配cu.meta_value而不是cu.meta_key。雖然無法檢查,因爲我沒有WP安裝。

+0

您正在合併,但當我使用時: LEFT JOIN wp_usermeta AS fn ON(cu.meta_key = fn.user_id AND fn.meta_key ='first_name') 'LEFT JOIN wp_usermeta AS ln ON(cu.meta_key = ln.user_id AND ln.meta_key ='last_name') 我還是#1054 - '字段列表'中的未知列'fn.meta_vale' –

+0

我想你有一個錯字那裏('fn.meta_vale',而不是'fn.meta_value')。 – TheWolf

+0

哇..就是這樣。謝謝! 也許它並不高於我的薪酬等級,只需要更多的信心。 –