Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
292 views
in Technique[技术] by (71.8m points)

php - Selecting the last 7 days from Now() in MYSQL

Thankyou for taking the time to look at my question.

I have this MYSQL query:

foreach( $wpdb->get_results(
    "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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...