我创建了一个与我原来的场景相同的示例表。数据库“test”中的表名“records”
数据库时区设置为 UTC (SET time_zone = "+00:00";)
`records` (`id`, `name`, `time_created`) (1, '议案', '2017-09-13 16:20:41'), (2, '议案', '2017-09-13 16:20:57'), (3, '议案', '2017-09-13 16:21:24'), (4, '议案', '2017-09-13 16:21:40'), (5, '议案', '2017-09-13 16:26:38'), (6, '议案', '2017-09-13 17:09:00'), (7, '议案', '2017-09-13 17:09:16'), (8, '议案', '2017-09-13 22:14:37'), (9, '议案', '2017-09-13 22:23:53'), (10, '议案', '2017-09-13 22:24:08'), (11, '议案', '2017-09-13 22:24:24'), (12, '议案', '2017-09-13 23:45:17'), (13, '议案', '2017-09-13 23:45:36'), (14, '议案', '2017-09-13 23:45:54'), (15, '议案', '2017-09-14 00:07:09'), (16, '议案', '2017-09-14 00:07:24'), (17, '议案', '2017-09-14 00:07:42'), (18, '议案', '2017-09-19 09:42:11'), (19, '议案', '2017-09-19 09:42:27'), (20, '议案', '2017-09-19 09:42:44'), (21, '议案', '2017-09-19 11:21:08'), (22, '议案', '2017-09-19 11:21:23'), (23, '议案', '2017-09-19 11:21:38'), (24, '议案', '2017-09-19 11:21:54'), (25, '议案', '2017-09-19 11:48:13'), (26, '议案', '2017-09-13 16:20:41'), (27, '议案', '2017-09-13 16:20:57'), (28, '议案', '2017-09-13 16:21:24'), (29, '议案', '2017-09-13 16:21:40'), (30, '议案', '2017-09-13 16:26:38'), (31, '议案', '2017-09-13 17:09:00'), (32, '议案', '2017-09-13 17:09:16'), (33, '议案', '2017-09-13 22:14:37'), (34, '议案', '2017-09-13 22:23:53'), (35, '议案', '2017-09-13 22:24:08'), (36, '议案', '2017-09-13 22:24:24'), (37, '议案', '2017-09-13 23:45:17'), (38, '议案', '2017-09-13 23:45:36'), (39, '议案', '2017-09-13 23:45:54'), (40, '议案', '2017-09-14 00:07:09'), (41, '议案', '2017-09-14 00:07:24'), (42, '议案', '2017-09-14 00:07:42'), (43, '议案', '2017-09-19 09:42:11'), (44, '议案', '2017-09-19 09:42:27'), (45, '议案', '2017-09-19 09:42:44'), (46, '议案', '2017-09-19 11:21:08'), (47, '议案', '2017-09-19 11:21:23'), (48, '议案', '2017-09-19 11:21:38'), (49, '议案', '2017-09-19 11:21:54'), (50, '议案', '2017-09-19 11:48:13');
我必须使用 CONVERT_TZ( mysql 转换函数)使用时间转换进行两个查询
我需要两个查询:1. 获取日期“今天”和“今天 - 30 天前”之间的记录 2. 获取给定日期的记录,例如“2017-09-14”
我尝试过以下查询
- 日期之间的记录:
SELECT * FROM test.records WHERE name LIKE '%motion%' AND CONVERT_TZ(time_created ,'+00:00','-7:0') BETWEEN DATE_SUB(CONVERT_TZ('2017-09-20 11:48:13' ,'+00:00','-7:0' ), 间隔 30 天) 和 CONVERT_TZ('2017-09-20 11:48:13','+00:00','-7:0') GROUP BY DATE(time_created) ORDER BY ID DESC; 结果 : 18议案 2017-09-19 09:42:11 15 议案 2017-09-14 00:07:09 1 议案 2017-09-13 16:20:41
- 给定日期的记录:
SELECT name,id, CONVERT_TZ(time_created ,'+00:00','-7:0') as time_created, DATE_FORMAT( CONVERT_TZ(time_created ,'+00:00','-7:0') , '%h:%i:%s %p') as new_format_time FROM test.records WHERE name LIKE '%motion%' AND DATE(CONVERT_TZ(time_created,'+00:00','-7:0')) = '2017-09-14' ORDER BY ID DESC 结果: 0 条记录