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只查询七条
时间: 2024-02-10 15:12:50 浏览: 111
DMTR-221:LDM-503-xxx:访问LSP中已处理的HSC公共数据
你可以将原来的查询语句中的 `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` 子句限制只查询七条记录。
阅读全文