开发者

Selecting the last 7 days from Now() in MYSQL

开发者 https://www.devze.com 2023-04-03 15:08 出处:网络
Thankyou for taking the time to look at my question. I have this MYSQL query: foreach( $wpdb->get_results(

Thankyou for taking the time to look at my question.

I have this MYSQL query:

foreach( $wpdb->get_results(
    开发者_如何学Python"SELECT wp_pixelcart_calendar.datefield AS DATE,
    IFNULL(SUM(wp_pixelcart_daily_sales.quantity),0) AS total_sales
    FROM wp_pixelcart_daily_sales RIGHT JOIN wp_pixelcart_calendar ON (DATE(wp_pixelcart_daily_sales.order_date) = wp_pixelcart_calendar.datefield)
    WHERE (wp_pixelcart_calendar.datefield BETWEEN (SELECT MIN(DATE(order_date)) FROM wp_pixelcart_daily_sales) AND (SELECT MAX(DATE(order_date)) FROM wp_pixelcart_daily_sales))
    GROUP BY DATE"
) as $key => $row) {

echo "<br>". $row->DATE . "',". $row->total_sales . "],";

}

I'm having a hard time to display the last seven days from now in the query, ive been playing around with:

BETWEEN (SELECT MIN(DATE(order_date)) FROM wp_pixelcart_daily_sales) AND (SELECT MAX(DATE(order_date)) FROM wp_pixelcart_daily_sales))

To this:

BETWEEN NOW() FROM wp_pixelcart_daily_sales) AND DATE_ADD(NOW(), INTERVAL 7 DAY) FROM wp_pixelcart_daily_sales))

But this doesn't seem to work.

Any help appreciated.

Thanks


if this is not working, returning 0 results, consider swapping the dates' range:

BETWEEN DATE_SUB(NOW(), INTERVAL 7 DAY) AND NOW()


You could just write:

SELECT * FROM table WHERE date_field > DATE_SUB(NOW(), INTERVAL 7 DAY)


WHERE order_date <= NOW() AND order_date >= DATE_SUB(order_date, INTERVAL 7 DAY)
0

精彩评论

暂无评论...
验证码 换一张
取 消