无法获得复杂的mysql查询

时间:2013-09-02 02:21:15

标签: mysql sql wordpress woocommerce

我正在尝试编写一个mysql查询,我遇到了一些问题。我正试图从我的Wordpress数据库中查询WooCommerce数据。基本发票数据存储在wp_posts表中,其余数据存储在wp_postmeta表中。现在,wp_posts表中的1个inovice指向wp_postmeta表中的多个项目。这是一个例子。

WP_POSTS

----------------------------------------------------
ID           STATUS                 Date
----------------------------------------------------
0001         OPEN                   01/01/2000
0002         OPEN                   01/01/2000
0003         CLOSED                 01/02/2000

WP_POSTMETA

--------------------------------------------------------------------------
ID        POST_ID               META_KEY                META_VALUE
--------------------------------------------------------------------------
0001      0001                  CustomerLN              Test
0002      0001                  CustomerFN              Tester
0003      0001                  Payment_Type            PayPal
0004      0001                  Invoice_Total           $200
0005      0002                  CustomerLN              Doe
0006      0002                  CustomerFN              John
0007      0002                  Payment_Type            CC-Mastercard
0008      0002                  Invoice_Total           $1000

我有一个从wp_posts表中提取数据的基本查询,但我无法弄清楚如何根据META_KEY值从第二个表中提取数据。

任何帮助都会很棒。提前谢谢。

4 个答案:

答案 0 :(得分:2)

首先,我会做

select a.*, b.* from WP_POSTS a, wp_postmeta b
where b.POST_ID = a.id

你做了什么?

答案 1 :(得分:2)

对于这种情况,您想使用“JOIN”。查看http://www.w3schools.com/sql/sql_join.asp,这几乎就是您想要做的。

它会结束如下:

SELECT *
FROM wp_posts
INNER JOIN wp_postmeta
ON wp_posts.ID=wp_postmeta.PostId;

另请注意,第二个表的表名错误。

答案 2 :(得分:1)

欢迎来到 Entity-Attribute-Value 世界。

要获得结果集的正常表示,您可能不仅需要JOIN这两个表,还需要PIVOT结果集。您可以使用

之类的查询来完成此操作
SELECT p.id, p.status,
       MAX(CASE WHEN m.meta_key = 'CustomerLN'    
                THEN m.meta_value END) customer_last_name,
       MAX(CASE WHEN m.meta_key = 'CustomerFN'    
                THEN m.meta_value END) customer_firt_name,
       MAX(CASE WHEN m.meta_key = 'Payment_Type'  
                THEN m.meta_value END) payment_type,
       MAX(CASE WHEN m.meta_key = 'Invoice_Total' 
                THEN m.meta_value END) invoice_total
 FROM wp_posts p LEFT JOIN wp_postmeta m
   ON p.id = m.post_id
 GROUP BY p.id, p.status

示例输出:

+------+--------+--------------------+--------------------+---------------+---------------+
| id   | status | customer_last_name | customer_firt_name | payment_type  | invoice_total |
+------+--------+--------------------+--------------------+---------------+---------------+
|    1 | OPEN   | Test               | Tester             | PayPal        | $200          |
|    2 | OPEN   | Doe                | John               | CC-Mastercard | $1000         |
|    3 | CLOSED | NULL               | NULL               | NULL          | NULL          |
+------+--------+--------------------+--------------------+---------------+---------------+

这是 SQLFiddle 演示


现在,您可以根据元键和元值过滤您的记录,您必须使用HAVING子句

例如,如果您想获得客户Jhon Doe的发票

SELECT p.id, p.status,
       MAX(CASE WHEN m.meta_key = 'CustomerLN'    
                THEN m.meta_value END) customer_last_name,
       MAX(CASE WHEN m.meta_key = 'CustomerFN'    
                THEN m.meta_value END) customer_first_name,
       MAX(CASE WHEN m.meta_key = 'Payment_Type'  
                THEN m.meta_value END) payment_type,
       MAX(CASE WHEN m.meta_key = 'Invoice_Total' 
                THEN m.meta_value END) invoice_total
 FROM wp_posts p LEFT JOIN wp_postmeta m
   ON p.id = m.post_id
 GROUP BY p.id, p.status
HAVING customer_last_name = 'Doe'
   AND customer_first_name = 'John'

输出:

+------+--------+--------------------+---------------------+---------------+---------------+
| id   | status | customer_last_name | customer_first_name | payment_type  | invoice_total |
+------+--------+--------------------+---------------------+---------------+---------------+
|    2 | OPEN   | Doe                | John                | CC-Mastercard | $1000         |
+------+--------+--------------------+---------------------+---------------+---------------+

这是 SQLFiddle 演示

答案 3 :(得分:0)

首先,您不应该尝试加入两个表,因为它们的表结构不同。让我们创建一个使用WP_POSTMETA数据的视图,并对其进行整合。

对于您感兴趣的每个X字段,将WP_POSTMETA与其自身连接X-1次,为每个Post_ID生成0到X行。使用WHERE子句从表的每个副本中获取1列,并将每Post_ID行的总行数减少为1(或无)。

过滤掉垃圾和vo!

注意:[table_name2]可以称为temp0[table_name3]称为temp1[table_name4]称为temp2,或任何您想要的。

SELECT WP_POSTMETA.Post_ID AS Post_ID, WP_POSTMETA.CustomerLN AS CustomerLN, [table_name2].CustomerFN AS  CustomerFN, [table_name3].Payment_Type AS Payment_Type, [table_name4].Meta_Value AS  Invoice_Total 
FROM WP_POSTMETA 
JOIN WP_POSTMETA AS [table_name2] ON WP_POSTMETA.Post_ID = [table_name2].Post_ID 
JOIN WP_POSTMETA AS [table_name3] ON WP_POSTMETA.Post_ID = [table_name3].Post_ID 
JOIN WP_POSTMETA AS [table_name4] ON WP_POSTMETA.Post_ID = [table_name4].Post_ID 
WHERE WP_POSTMETA.Meta_Value =  "CustomerLN"
AND [table_name2].Meta_Value =  "CustomerFN"
AND [table_name3].Meta_Value =  "Payment_Type"
AND [table_name4].Meta_Value =  "Invoice_Total"

这将生成一个输出,可以通过前置

使其成为view
CREATE VIEW WP_TRANSACTIONS AS

此视图JOIN已编辑为WP_POST

SELECT * FROM WP_POST 
LEFT JOIN WP_TRANSACTIONS ON WP_POST.Post_ID = WP_TRANSACTIONS.Post_ID