将查询合并为一个

时间:2013-04-28 00:33:45

标签: mysql

我在下面的查询中返回OrderID,order_item_id,_product_id,Quantity。

SELECT `wp_woocommerce_order_items`.`order_id` AS 'OrderID',
    `wp_woocommerce_order_items`.`order_item_id`,
    MAX(CASE WHEN `wp_woocommerce_order_itemmeta`.`meta_key` = '_product_id' THEN `wp_woocommerce_order_itemmeta`.`meta_value` ELSE NULL END) AS '_product_id',
    MAX(CASE WHEN `wp_woocommerce_order_itemmeta`.`meta_key` = '_qty' THEN `wp_woocommerce_order_itemmeta`.`meta_value` ELSE NULL END) AS 'Quantity'
FROM `wp_woocommerce_order_items`, `wp_woocommerce_order_itemmeta`
WHERE `wp_woocommerce_order_items`.`order_id` = 50
AND `wp_woocommerce_order_items`.`order_item_id` = `wp_woocommerce_order_itemmeta`.`order_item_id`
AND `wp_woocommerce_order_items`.`order_item_type` = 'line_item'
AND (`wp_woocommerce_order_itemmeta`.`meta_key` = '_qty'
    OR `wp_woocommerce_order_itemmeta`.`meta_key` = '_product_id')
GROUP BY `wp_woocommerce_order_items`.`order_item_id`

返回:

+---------+---------------+-------------+----------+
| OrderID | order_item_id | _product_id | Quantity |
+---------+---------------+-------------+----------+
|      50 |            11 | 27          | 3        |
|      50 |            18 | 42          | 1        |
+---------+---------------+-------------+----------+

然后我在php的上面结果中循环遍历每一行,使用下面查询中的_product_id来匹配wp_postmetapost_id

SELECT `post_id`,  
    MAX(CASE WHEN `wp_postmeta`.`meta_key` = '_sku' THEN `wp_postmeta`.`meta_value` ELSE NULL END) AS 'PartNumber',
    MAX(CASE WHEN `wp_postmeta`.`meta_key` = '_price' THEN `wp_postmeta`.`meta_value` ELSE NULL END) AS 'UnitPrice'
FROM `wp_postmeta`
WHERE `wp_postmeta`.`post_id` = $PRODUCT_ID
AND (`wp_postmeta`.`meta_key` = '_sku'
    OR `wp_postmeta`.`meta_key` = '_price')

返回:

+---------+------------+-----------+
| post_id | PartNumber | UnitPrice |
+---------+------------+-----------+
|      27 | MOO123     | 50        |
+---------+------------+-----------+

+---------+------------+-----------+
| post_id | PartNumber | UnitPrice |
+---------+------------+-----------+
|      42 | MOO345     | 145       |
+---------+------------+-----------+

有没有办法将两者结合起来,所以它会在第一个查询结果中添加PartNumberUnitPrice并返回如下所示的数据?

+---------+---------------+-------------+----------+------------+-----------+
| OrderID | order_item_id | _product_id | Quantity | PartNumber | UnitPrice |
+---------+---------------+-------------+----------+------------+-----------+
|      50 |            11 | 27          | 3        | MOO123     | 50        |
|      50 |            18 | 42          | 1        | MOO345     | 145       |
+---------+---------------+-------------+----------+------------+-----------+

此处的表格数据:http://pastebin.com/3EN5P4fD

由于

1 个答案:

答案 0 :(得分:1)

如何使用JOIN声明?(请参阅下面有关JOIN问题的脚注。

SELECT `i`.`order_id` AS 'OrderID',
       `i`.`order_item_id`,
       @productID := (SELECT MAX(
                CASE 
                    WHEN `meta_key` = '_product_id' 
                    THEN `meta_value` 
                    ELSE NULL 
                END)
           FROM `wp_woocommerce_order_itemmeta`
          WHERE `order_item_id` = `i`.`order_item_id`
            AND (`meta_key` = '_qty'
                 OR `meta_key` = '_product_id')) AS '_product_id',

       (SELECT MAX(
                   CASE 
                       WHEN `meta_key` = '_qty' 
                       THEN `meta_value` 
                       ELSE NULL 
                   END) 
          FROM `wp_woocommerce_order_itemmeta`
         WHERE `order_item_id` = `i`.`order_item_id`
           AND (`meta_key` = '_qty'
                OR `meta_key` = '_product_id')) AS 'Quantity',

       (SELECT MAX(
                   CASE 
                       WHEN `meta_key` = '_sku' 
                       THEN `meta_value` 
                       ELSE NULL 
               END)
          FROM `wp_postmeta`
         WHERE `post_id` = @productID
           AND (`meta_key` = '_sku'
                OR `meta_key` = '_price')) AS 'PartNumber',

       (SELECT MAX(
                   CASE 
                       WHEN `meta_key` = '_price' 
                       THEN `meta_value` 
                   ELSE NULL 
               END)
          FROM `wp_postmeta`
         WHERE `post_id` = @productID
           AND (`meta_key` = '_sku'
                OR `meta_key` = '_price')) AS 'UnitPrice'

 FROM `wp_woocommerce_order_items` AS `i` 
WHERE `i`.`order_id` = 50
  AND `i`.`order_item_type` = 'line_item'
GROUP BY `i`.`order_item_id`

我没有测试过上述声明,但它应该让你开始。

Stack Overflow网站上还有另一篇文章:MySQL: Use CASE/ELSE value as join parameter,其中讨论了如何将CASE用作JOIN参数。看看吧!

<强>脚注:

由于错误,我对JOIN语句的初始尝试失败:“错误代码:1111。无效使用组函数”。阅读帖子MySQL LEFT JOIN, GROUP BY and ORDER BY not working as required后,我将JOIN语句更改为子查询。