将表连接到自己和彼此

时间:2013-10-17 20:24:29

标签: php mysql sql wordpress

我大部分时间都在努力解决这个问题所以我想是时候问专家了: 我正在尝试在WordPress之外访问我的WordPress数据库,所以我正在编写查询以从稍微复杂的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.meta_key ='_ customer_user')= fn.user_id AND fn.meta_key ='first_name')以及许多其他子查询等。

显然我现在高于我在SQL查询中的薪水等级,所以任何帮助都会很棒。谢谢!

1 个答案:

答案 0 :(得分: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。