[英]Select rows from table inserted through the past week
I need to select all rows added in the last week in the database. 我需要选择数据库中上周添加的所有行。
This is for a "Top 5" page which should show the most sold products over the past 7 days. 这是针对“前5名”页面的,该页面应显示过去7天销售最多的产品。 I tried:
我试过了:
SELECT order_id
FROM orders
WHERE order_date BETWEEN DATE_ADD(week,-1,CURRENT_DATE) AND NOW()
which returns this error: 返回此错误:
1064 - You have an error in your SQL syntax;
1064-您的SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near '-1,CURRENT_DATE) AND NOW() LIMIT 0, 25' at line 1
检查与您的MySQL服务器版本对应的手册以获取正确的语法,以在第1行的'-1,CURRENT_DATE)AND NOW()LIMIT 0,25'附近使用
Other things I have tried were simply the same query but with other syntax not working on this sql server. 我尝试过的其他操作只是相同的查询,但是在此sql服务器上无法使用其他语法。
you just need below 你只需要下面
SELECT order_id FROM orders
WHERE order_date >= NOW() - INTERVAL 1 WEEK
If you are using Mysql try this to get last weeks data 如果您使用的是Mysql,请尝试此操作以获取上周的数据
SELECT order_id FROM orders WHERE date >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY
AND date < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.