我有以下查询来提取订单的所有订单:
SELECT
order_id,
GROUP_CONCAT(order_item_name SEPARATOR ' | ') as 'items'
FROM
wp_woocommerce_order_items oi1
WHERE
oi1.order_item_type = 'line_item'
AND order_id = 422
GROUP BY
order_id
它返回我想要的结果:
我现在要做的是从另一个表中添加如下所示的数据:
SELECT * FROM wp_woocommerce_order_itemmeta where order_item_id = 17
wp_woocommerce_order_itemmeta
和wp_woocommerce_order_items
加入order_item_id
所以我最终的期望结果看起来像是:
order_item_name:_qty:_line_subtotal
order_id | items
--------- -----------------------------------
422 | Metal Polish:1:13.09
答案 0 :(得分:0)
你需要这样的吗?
SELECT * FROM wp_woocommerce_order_itemmeta as wc
inner join
(SELECT
order_id,
GROUP_CONCAT(order_item_name SEPARATOR ' | ') as 'items'
FROM
wp_woocommerce_order_items oi1
WHERE
oi1.order_item_type = 'line_item'
AND order_id = 422
GROUP BY
order_id) as order_counts
using (order_id)
答案 1 :(得分:0)
以下是仅使用一个LEFT JOIN和一个CONCAT
的示例SELECT
oi1.order_id,
GROUP_CONCAT(
CONCAT(oi1.order_item_name , ':', ii.meta_value)
SEPARATOR ' | ') as 'items'
FROM
wp_woocommerce_order_items oi1
LEFT JOIN
wp_woocommerce_order_itemmeta i ON i.order_item_id = oi1.order_item_id AND i.meta_key= '_line_subtotal'
WHERE
oi1.order_item_type = 'line_item'
AND oi1.order_id = 422
GROUP BY
oi1.order_id;
答案 2 :(得分:0)
由于wp_woocommerce_order_itemmeta
中的每个值都在自己的行中,因此您需要多次加入该表。
SELECT order_id, GROUP_CONCAT(
order_item_name,':',qty.meta_value,':',subtotal.meta_value SEPARATOR ' | '
) as 'items'
FROM wp_woocommerce_order_items oi1
JOIN wp_woocommerce_order_itemmeta qty ON
qty.order_item_id = oli.order_item_id
AND qty.meta_key = '_qty'
JOIN wp_woocommerce_order_itemmeta subtotal ON
qty.order_item_id = oli.order_item_id
AND qty.meta_key = '_line_subtotal'
WHERE oi1.order_item_type = 'line_item'
AND order_id = 422
GROUP BY order_id