简体   繁体   English

从过去一周插入的表格中选择行

[英]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.

相关问题 DB2-从表中选择最后插入的5行 - DB2 - select last inserted 5 rows from a table PostgreSQL - 根据从另一个表中选择插入行,并使用新插入的行更新该表中的 FK - PostgreSQL - insert rows based on select from another table, and update an FK in that table with the newly inserted rows 选择少于一周的PostgreSQL表行 - Select PostgreSQL table rows younger than a week 如何遍历 SQL 中的分区,收集特定周的行并使用收集的行创建新表 - How to loop through partitions in SQL, collect rows from a specific week and create a new table with the collected rows MySql:当星期是实际星期,而前一周是星期几时,从表中选择 - MySql: Select from a table when week is actual week, and the week before 如何在过去2年的Oracle中从表中选择10行 - How do I select 10 rows from a table for past 2 years in Oracle 从过去60天以来未制作时间戳的表中选择行 - Select rows from a table where a timestamp has not been made since the past 60 days MySQL的:标记转储表中插入的行 - Mysql: tag inserted rows from dump table 将表中的行转换为一周中的几天 - Converting rows from a table into days of the week 从常量插入的表中选择 - Select From a table that is constantly being inserted into
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM