我被困在这个查询中一段时间,任何类型的帮助将不胜感激。这是一个使用 woocommerce 的 wordpress 网站。
尝试获取客户的购买详细信息,我需要获取应包含以下字段的结果集。
Email | First Name | Orders | Items Purchased | Order Total
我为此编写了以下 SQL。 achg8 是表前缀。一个订单可能包含一件或多件商品。
SELECT
pm1.meta_value as email ,
pm2.meta_value as first_name,
sum(pm3.meta_value) as total,
count(posts.ID) as orders ,
count(items.order_item_id) as items
from achg8_posts as posts
left join achg8_postmeta as pm1 on posts.ID = pm1.post_id
left join achg8_postmeta as pm2 on posts.ID = pm2.post_id
left join achg8_postmeta as pm3 on posts.ID = pm3.post_id
left join achg8_woocommerce_order_items as items on items.order_id = posts.ID
WHERE
posts.post_type = 'shop_order' and
posts.post_status = 'wc-processing' and
pm1.meta_key='_billing_email' and
pm2.meta_key='_billing_first_name' and
pm3.meta_key='_order_total'
GROUP by email
问题在于订单号和商品号始终相等。但实际情况并非如此。
需要了解我做错了什么。
根据@matigo 的评论将 sql 编辑为以下内容。现在的问题是订单总额被多次加起来。例如,如果一个订单有两个项目(即连接结果集中的两行)订单总额被加起来两次。
SELECT
pm1.meta_value as email ,
pm2.meta_value as first_name,
sum(pm3.meta_value) as total,
count(DISTINCT posts.ID) as orders ,
count(items.order_item_id) as items
from achg8_posts as posts
left join achg8_postmeta as pm1 on posts.ID = pm1.post_id
left join achg8_postmeta as pm2 on posts.ID = pm2.post_id
left join achg8_postmeta as pm3 on posts.ID = pm3.post_id
left join achg8_woocommerce_order_items as items on items.order_id = posts.ID
WHERE
posts.post_type = 'shop_order' and
posts.post_status = 'wc-processing' and
pm1.meta_key='_billing_email' and
pm2.meta_key='_billing_first_name' and
pm3.meta_key='_order_total'
GROUP by email
答案 0 :(得分:1)
假设相同的订单号可以在 items
中出现多次,那么您的 DISTINCT
中似乎需要一个 COUNT
。
试试这个:
SELECT pm1.`meta_value` as `email`,
pm2.`meta_value` as `first_name`,
SUM(pm3.`meta_value`) as `total`,
COUNT(posts.`ID`) as `orders`,
(SELECT COUNT(z.`order_item_id`)
FROM `achg8_woocommerce_order_items` z
WHERE posts.`ID` = z.`order_id`) as `items`
FROM `achg8_posts` posts INNER JOIN `achg8_postmeta` pm1 ON posts.`ID` = pm1.`post_id`
INNER JOIN `achg8_postmeta` pm2 ON posts.`ID` = pm2.`post_id`
INNER JOIN `achg8_postmeta` pm3 ON posts.`ID` = pm3.`post_id`
WHERE posts.`post_type` = 'shop_order'
and posts.`post_status` = 'wc-processing'
and pm1.`meta_key` = '_billing_email'
and pm2.`meta_key` = '_billing_first_name'
and pm3.`meta_key` = '_order_total'
GROUP by `email`, `first_name`
这应该给你你正在寻找的东西??
答案 1 :(得分:1)
你可以试试这个,看看它是否有效。
抱歉,我没有考虑与 meta_key 的关系就删除了那个 join,你可以试试这个。
SELECT
pm1.`meta_value` as `email`,
pm2.`meta_value` as `first_name`,
SUM(pm3.`meta_value`) as `total`,
COUNT(distinct posts.`ID`) as `orders`,
COUNT(distinct items.order_item_id) as `items`
FROM `achg8_posts` posts
INNER JOIN `achg8_postmeta` pm1 ON posts.`ID` = pm1.`post_id`
INNER JOIN `achg8_postmeta` pm2 ON posts.`ID` = pm2.`post_id`
INNER JOIN `achg8_postmeta` pm3 ON posts.`ID` = pm3.`post_id`
INNER JOIN achg8_woocommerce_order_items as items on items.order_id = posts.ID
WHERE
posts.`post_type` = 'shop_order' and
posts.`post_status` = 'wc-processing' and
pm1.`meta_key` = '_billing_email' and
pm2.`meta_key` = '_billing_first_name' and
pm3.`meta_key` = '_order_total'
GROUP by `email`