sql for woocommerce用于确定特定日期的第一个订单

gupuwyp2  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(259)

我试图创建一个sql查询来确定某一天客户的第一批订单数。我已经创建了一个sql查询,它在给定的日期向我提供订单,但是我已经确定该查询允许来自在给定日期之前已订购的客户的订单进入结果。
我的问题是:

SELECT
    users.ID,
    users.display_name,
    MIN( orders.post_date ) AS 'first order date',
    orders.post_status AS 'status' 
FROM
    wp_users AS users
    INNER JOIN wp_postmeta AS customer_ids ON users.ID = customer_ids.meta_value 
    AND customer_ids.meta_key = '_customer_user'
    INNER JOIN wp_posts AS orders ON customer_ids.post_id = orders.ID 
    AND orders.post_type = 'shop_order' 
    AND orders.post_status = 'wc-completed' 
WHERE   
    orders.post_date BETWEEN '2020-05-31 00:00:00' AND '2020-05-31 23:59:59'
GROUP BY
    users.ID
ORDER BY
    MIN( orders.post_date )

结果如下:

5746    Dave Shapiro        2020-05-31 06:29:39 wc-completed
7090    srcb57              2020-05-31 07:14:20 wc-completed
2407    Michael P Parks     2020-05-31 11:01:30 wc-completed
5849    Adam Lassen         2020-05-31 11:11:28 wc-completed
6706    espenzone           2020-05-31 15:28:23 wc-completed
7148    Ledyard McFadden    2020-05-31 16:35:29 wc-completed
5066    Jeff Phillips       2020-05-31 21:17:01 wc-completed

然而,我知道一个事实,客户5066和6706在过去有订单。
有没有办法强制查询在日期范围内计算以下语句?

MIN( orders.post_date ) AS 'first order date'

我试图添加以下内容,但在group by中出现错误(1111-group函数的使用无效)

SELECT
    users.ID,
    users.display_name,
    MIN( orders.post_date ) AS 'first order date',
    orders.post_status AS 'status' 
FROM
    wp_users AS users
    INNER JOIN wp_postmeta AS customer_ids ON users.ID = customer_ids.meta_value 
    AND customer_ids.meta_key = '_customer_user'
    INNER JOIN wp_posts AS orders ON customer_ids.post_id = orders.ID 
    AND orders.post_type = 'shop_order' 
    AND orders.post_status = 'wc-completed' 
WHERE   
    MIN( orders.post_date ) BETWEEN '2020-05-31 00:00:00' AND '2020-05-31 23:59:59'
GROUP BY
    users.ID
ORDER BY
    MIN( orders.post_date )

输出错误:

SELECT
    users.ID,
    users.display_name,
    MIN( orders.post_date ) AS 'first order date',
    orders.post_status AS 'status' 
FROM
    wp_users AS users
    INNER JOIN wp_postmeta AS customer_ids ON users.ID = customer_ids.meta_value 
    AND customer_ids.meta_key = '_customer_user'
    INNER JOIN wp_posts AS orders ON customer_ids.post_id = orders.ID 
    AND orders.post_type = 'shop_order' 
    AND orders.post_status = 'wc-completed' 
WHERE   
    MIN( orders.post_date ) BETWEEN '2020-05-31 00:00:00' AND '2020-05-31 23:59:59'
GROUP BY
    users.ID
ORDER BY
    MIN( orders.post_date )
> 1111 - Invalid use of group function
cgfeq70w

cgfeq70w1#

当你聚集一个值时,基本上你必须按所有列分组,

SELECT
    users.ID,
    users.display_name,
    MIN( orders.post_date ) AS 'first order date',
    orders.post_status AS 'status' 
FROM
    wp_users AS users
    INNER JOIN wp_postmeta AS customer_ids ON users.ID = customer_ids.meta_value 
    AND customer_ids.meta_key = '_customer_user'
    INNER JOIN wp_posts AS orders ON customer_ids.post_id = orders.ID 
    AND orders.post_type = 'shop_order' 
    AND orders.post_status = 'wc-completed' 
       GROUP BY
users.ID,
users.display_name,
orders.post_status
having MIN( orders.post_date ) BETWEEN '2020-05-31 00:00:00' AND '2020-05-31 23:59:59'
ORDER BY
MIN( orders.post_date )

相关问题