自定义WordPress SQL WC查询插件

发布于 2025-02-01 13:55:35 字数 1113 浏览 2 评论 0 原文

我对WordPress中一些SQL查询有一个问题。该对象是从多个表及其列中获取以下信息。

我需要:<代码>订购时间|日期和时间|名称|电子邮件|部门|总订单|金额

我已经为自己提供了以下视觉帮助:“

wp_postmeta 包含:

  • 部门 as = billing_field_267
  • 电子邮件 as = billing_email
  • 名字
  • pland> last Name>

= _billing_first_name > wp_wc_order_stats 包含:

  • 日期和时间 = date_created
  • 订购时间 = billing_field_739
  • = ??
  • 总订单 = ??

计费字段是meta_keys,所以我只需要它们的价值。

我已经尝试了以下SQL语句以获取我的需求:

SELECT 
  user.meta_key, user.meta_value
FROM
  wp_postmeta AS user, wp_wc_order_stats as order
WHERE
  user.meta_key='billing_field_267', user.meta_key='billing_email', user.meta_key='_billing_first_name', user.meta_key='_billing_last_name', user.meta_key='billing_field_739'
AND
  order.date_created

它似乎并没有根据需要在正确的直接方面工作。

I have a question regarding some SQL queries in WordPress. The object is getting the following information from multiple tables and their columns.

I need: Ordered Time | Date and Time | Name | Email | Department | Total Orders | Amount

I've scoped out the following as a visual aid for myself: Text

wp_postmeta contains:

  • Department as = billing_field_267
  • Email as = billing_email
  • First Name = _billing_first_name
  • Last Name = _billing_last_name

wp_wc_order_stats contains:

  • Date and Time = date_created
  • Ordered Time = billing_field_739
  • Amount = ??
  • Total Orders = ??

The billing fields are meta_keys so I just need their values.

I've tried the following SQL Statement to get what I need:

SELECT 
  user.meta_key, user.meta_value
FROM
  wp_postmeta AS user, wp_wc_order_stats as order
WHERE
  user.meta_key='billing_field_267', user.meta_key='billing_email', user.meta_key='_billing_first_name', user.meta_key='_billing_last_name', user.meta_key='billing_field_739'
AND
  order.date_created

It doesn't seem to be working as needed some help in the right direct would be apprecaited.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅唱々樱花落 2025-02-08 13:55:35

您有两个必须与之合作的查询步骤。

查询一个是第一个子问题相当简单。构建客户信息的第二个子查询需要将转换为 ,在在这里

因此,假设总订单是有问题的订单数(s)和金额 为了成为订单的总价值,以下查询应起作用。


SELECT ordInfo.OrdTime as 'order Time',
       ordInfo.DateNTime as 'Date and Time',
       custInfo.Name,
       custInfo.Email,
       custInfo.Department,
       ordInfo.OrdCnt as 'Total orders',
       ordInfo.Amount
FROM
(
    SELECT ord.customer_id as 'customer_id', 
            ord.date_created as 'DateNTime', 
            orderMeta.meta_value as 'OrdTime',
            COUNT(ord.order_id) as 'OrdCnt', 
            SUM(total_sales + tax_total + shipping_total) as  'Amount'
    FROM wp_wc_order_stats ord 
        LEFT JOIN wp_postmeta orderMeta ON ord.order_id = orderMeta.post_id AND orderMeta.meta_key = 'billing_field_739'
    GROUP BY customer_id, date_created, meta_value
)ordInfo LEFT JOIN 
(
    SELECT user_id, 
        COALESCE(MAX(CASE meta_key WHEN 'billing_field_267' THEN meta_value END), ' ') as 'Department',
        COALESCE(MAX(CASE meta_key WHEN 'billing_email' THEN meta_value END), ' ') as 'Email',
        CONCAT(
            COALESCE(MAX(CASE meta_key WHEN '_billing_first_name' THEN meta_value END), ' '), ' ' ,
            COALESCE(MAX(CASE meta_key WHEN '_billing_last_name' THEN meta_value END), ' ')
        ) as 'NAME'
    FROM wp_usermeta
    WHERE meta_key IN ('billing_field_267', 'billing_email', '_billing_first_name', '_billing_last_name')
    GROUP BY user_id
)custInfo
ON ordInfo.customer_id = custInfo.user_id

You have two steps of query you have to work with.

Query one which is the first sub-query is fairly straight forward. The second subquery which build customers' info requires the conversion of ROWs into COLUMNs in the table which is described in greater detail in here

Thus, assuming Total Orders is number of order(s) in question and Amount to be total value of the order(s), the following query should work.


SELECT ordInfo.OrdTime as 'order Time',
       ordInfo.DateNTime as 'Date and Time',
       custInfo.Name,
       custInfo.Email,
       custInfo.Department,
       ordInfo.OrdCnt as 'Total orders',
       ordInfo.Amount
FROM
(
    SELECT ord.customer_id as 'customer_id', 
            ord.date_created as 'DateNTime', 
            orderMeta.meta_value as 'OrdTime',
            COUNT(ord.order_id) as 'OrdCnt', 
            SUM(total_sales + tax_total + shipping_total) as  'Amount'
    FROM wp_wc_order_stats ord 
        LEFT JOIN wp_postmeta orderMeta ON ord.order_id = orderMeta.post_id AND orderMeta.meta_key = 'billing_field_739'
    GROUP BY customer_id, date_created, meta_value
)ordInfo LEFT JOIN 
(
    SELECT user_id, 
        COALESCE(MAX(CASE meta_key WHEN 'billing_field_267' THEN meta_value END), ' ') as 'Department',
        COALESCE(MAX(CASE meta_key WHEN 'billing_email' THEN meta_value END), ' ') as 'Email',
        CONCAT(
            COALESCE(MAX(CASE meta_key WHEN '_billing_first_name' THEN meta_value END), ' '), ' ' ,
            COALESCE(MAX(CASE meta_key WHEN '_billing_last_name' THEN meta_value END), ' ')
        ) as 'NAME'
    FROM wp_usermeta
    WHERE meta_key IN ('billing_field_267', 'billing_email', '_billing_first_name', '_billing_last_name')
    GROUP BY user_id
)custInfo
ON ordInfo.customer_id = custInfo.user_id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文