DATE_SUB(CURDATE(), INTERVAL 2 DAY)
时间: 2023-11-20 13:07:37 浏览: 143
This MySQL query subtracts 2 days from the current date and returns the resulting date. For example, if today is August 10th, 2021, the query would return August 8th, 2021.
相关问题
SELECT DATE_FORMAT( dmtr.`day`,'%Y-%m-%d') as oneday, dmtr.column_num, dmtr.table_num FROM dap_metadata_table_record AS dmtr GROUP BY oneday HAVING oneday >= DATE_SUB(CURDATE(), INTERVAL 7 DAY) UNION ALL SELECT d.`day`, 0, 0 FROM ( SELECT DATE_SUB(CURDATE(), INTERVAL 7 DAY) AS day UNION ALL SELECT DATE_SUB(CURDATE(), INTERVAL 6 DAY) UNION ALL SELECT DATE_SUB(CURDATE(), INTERVAL 5 DAY) UNION ALL SELECT DATE_SUB(CURDATE(), INTERVAL 4 DAY) UNION ALL SELECT DATE_SUB(CURDATE(), INTERVAL 3 DAY) UNION ALL SELECT DATE_SUB(CURDATE(), INTERVAL 2 DAY) UNION ALL SELECT DATE_SUB(CURDATE(), INTERVAL 1 DAY) ) AS d LEFT JOIN dap_metadata_table_record AS dmtr1 ON d.`day` = dmtr1.`day` WHERE dmtr1.`day` IS NULL ORDER BY oneday DESC; 修改sql只查询七条
你可以将原来的查询语句中的 `HAVING` 子句改为 `WHERE` 子句,并添加一个条件限制只查询最近七天的记录,修改后的 SQL 查询语句如下:
```
SELECT DATE_FORMAT(dmtr.`day`, '%Y-%m-%d') AS oneday, dmtr.column_num, dmtr.table_num
FROM dap_metadata_table_record AS dmtr
WHERE dmtr.`day` >= DATE_SUB(CURDATE(), INTERVAL 7 DAY)
GROUP BY oneday
UNION ALL
SELECT d.`day`, 0, 0
FROM (
SELECT DATE_SUB(CURDATE(), INTERVAL 7 DAY) AS day
UNION ALL SELECT DATE_SUB(CURDATE(), INTERVAL 6 DAY)
UNION ALL SELECT DATE_SUB(CURDATE(), INTERVAL 5 DAY)
UNION ALL SELECT DATE_SUB(CURDATE(), INTERVAL 4 DAY)
UNION ALL SELECT DATE_SUB(CURDATE(), INTERVAL 3 DAY)
UNION ALL SELECT DATE_SUB(CURDATE(), INTERVAL 2 DAY)
UNION ALL SELECT DATE_SUB(CURDATE(), INTERVAL 1 DAY)
) AS d
LEFT JOIN dap_metadata_table_record AS dmtr1 ON d.`day` = dmtr1.`day`
WHERE dmtr1.`day` IS NULL
ORDER BY oneday DESC
LIMIT 7;
```
以上 SQL 查询语句中,`WHERE dmtr.`day` >= DATE_SUB(CURDATE(), INTERVAL 7 DAY)` 子句限制只查询最近七天的记录,`LIMIT 7` 子句限制只查询七条记录。
date_sub(date_sub(curdate(),interval 1 day),interval 1 year
你的问题是如何将当前日期减去一天再减去一年?可以使用以下MySQL语句实现:
```
SELECT DATE_SUB(DATE_SUB(CURDATE(), INTERVAL 1 DAY), INTERVAL 1 YEAR);
```
这将返回当前日期减去一天再减去一年的结果。请注意,这是一个示例,具体的语法可能因您使用的数据库系统而有所不同。
阅读全文