php 从 MYSQL 中的 Now() 选择过去 7 天

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7341658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:31:38  来源:igfitidea点击:

Selecting the last 7 days from Now() in MYSQL

phpmysql

提问by MattStrange

Thankyou for taking the time to look at my question.

谢谢你花时间看我的问题。

I have this MYSQL query:

我有这个 MYSQL 查询:

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:

我很难在查询中显示从现在开始的最后 7 天,我一直在玩:

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

谢谢

回答by Trevor

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

如果这不起作用,返回 0 结果,请考虑交换日期范围:

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

回答by Viesturs Knopkens

You could just write:

你可以写:

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

回答by Andy

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