SQL:汇总CASE语句返回错误结果

时间:2018-09-03 17:07:21

标签: sql wordpress woocommerce

自从我编写了复杂的SQL语句以来,已经很久了。我正在WordPress / WooCommerce中工作,试图对每个客户进行订单汇总。

当我开始使用此SQL语句进行测试时,只要获得包含客户信息的订单列表,它就可以工作:

SELECT wp_users.ID as userID,
       wp_posts.post_date,
       wp_posts.post_type,
       wp_users.user_email,
       MAX(CASE WHEN wp_usermeta.meta_key = 'billing_first_name' THEN wp_usermeta.meta_value END) as firstName,
       MAX(CASE WHEN wp_usermeta.meta_key = 'billing_last_name' THEN wp_usermeta.meta_value END) as lastName,
       MAX(CASE WHEN wp_woocommerce_order_itemmeta.meta_key = '_line_total' THEN wp_woocommerce_order_itemmeta.meta_value END) as orderTotal

FROM wp_posts
  INNER JOIN wp_postmeta
     ON wp_postmeta.post_id = wp_posts.ID
  INNER JOIN wp_users
     ON wp_postmeta.meta_key = '_customer_user'
     AND wp_users.ID = wp_postmeta.meta_value
  INNER JOIN wp_usermeta
     ON wp_usermeta.user_id = wp_users.ID
  INNER JOIN wp_woocommerce_order_items
     ON wp_woocommerce_order_items.order_id = wp_posts.ID
  INNER JOIN wp_woocommerce_order_itemmeta
     ON wp_woocommerce_order_itemmeta.order_item_id = wp_woocommerce_order_items.order_item_id
WHERE wp_posts.post_type IN ('shop_order','shop_subscription') 
GROUP BY wp_posts.ID

结果显示在此处的第一张图片中:https://imgur.com/a/mZs9Iej

如果我将这些结果汇总到excel中(到目前为止只有1个测试用户),则该用户的总费用为$ 4281。

如果我然后尝试对每个用户在SQL语句中进行SUM运算,例如此处(仅更改了两行,最后一个MAX更改为SUM,GROUP BY更改为wp_users.ID):

SELECT wp_users.ID as userID,
       wp_posts.post_date,
       wp_posts.post_type,
       wp_users.user_email,
       MAX(CASE WHEN wp_usermeta.meta_key = 'billing_first_name' THEN wp_usermeta.meta_value END) as firstName,
       MAX(CASE WHEN wp_usermeta.meta_key = 'billing_last_name' THEN wp_usermeta.meta_value END) as lastName,
       SUM(CASE WHEN wp_woocommerce_order_itemmeta.meta_key = '_line_total' THEN wp_woocommerce_order_itemmeta.meta_value END) as userTotal

FROM wp_posts
  INNER JOIN wp_postmeta
     ON wp_postmeta.post_id = wp_posts.ID
  INNER JOIN wp_users
     ON wp_postmeta.meta_key = '_customer_user'
     AND wp_users.ID = wp_postmeta.meta_value
  INNER JOIN wp_usermeta
     ON wp_usermeta.user_id = wp_users.ID
  INNER JOIN wp_woocommerce_order_items
     ON wp_woocommerce_order_items.order_id = wp_posts.ID
  INNER JOIN wp_woocommerce_order_itemmeta
     ON wp_woocommerce_order_itemmeta.order_item_id = wp_woocommerce_order_items.order_item_id
WHERE wp_posts.post_type IN ('shop_order','shop_subscription') 
GROUP BY wp_users.ID

它返回的总和为$ 340k,而不是$ 4k,太大,如下图所示:https://imgur.com/a/mZs9Iej

有人可以发现我犯的错误吗?任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:0)

尝试一下

SELECT users.ID as userID,
       posts.post_date,
       posts.post_type,
       users.user_email,
       fname.meta_value as firstName,
       lname.meta_value as lastName,
       SUM(itemmeta.meta_value) as userTotal
FROM wp_posts posts
  INNER JOIN wp_postmeta postmeta
     ON postmeta.post_id = posts.ID
     AND postmeta.meta_key = '_customer_user'
  INNER JOIN wp_users users
     ON users.ID = postmeta.meta_value
  INNER JOIN wp_woocommerce_order_items items
     ON items.order_id = posts.ID
  INNER JOIN wp_woocommerce_order_itemmeta itemmeta
     ON itemmeta.order_item_id = items.order_item_id
     AND itemmeta.meta_key = '_line_total'
  LEFT JOIN wp_usermeta fname
     ON fname.user_id = users.ID 
     AND fname.meta_key = 'billing_first_name'
  LEFT JOIN wp_usermeta lname
     ON lname.user_id = users.ID 
     AND lname.meta_key = 'billing_last_name'
WHERE posts.post_type IN ('shop_order','shop_subscription') 
GROUP BY users.ID

我为表引入了别名,并用左联接替换了您的case构造。

似乎您需要指定meta_keys作为条件,因为否则您将联接与meta_key不匹配的行。反过来,这会将wp_woocommerce_order_itemmeta中的联接行相乘,因此也就是您要总结的属性。