DATE_SUB(CURDATE()
时间: 2023-07-25 08:49:30 浏览: 80
DATE_SUB()函数用于做日期计算,可以将一个日期减去指定的时间间隔得到一个新的日期。CURDATE()函数用于获取当前日期。因此,DATE_SUB(CURDATE(), INTERVAL '12' MONTH)的含义是:从当前日期往前推12个月的日期。这个函数会返回一个日期类型的值,例如'2021-09-01'。
相关问题
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);
```
这将返回当前日期减去一天再减去一年的结果。请注意,这是一个示例,具体的语法可能因您使用的数据库系统而有所不同。
阅读全文