3

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

次に、上記の結果の各行をループして、以下のクエリで _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       |
+---------+------------+-----------+

2つを組み合わせる方法はありますか?最初のクエリ結果に and を追加し、以下のようなデータを返しますPartNumberか?UnitPrice

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

ありがとう

4

1 に答える 1