體溫表:sql
-- 體溫: Temperature DROP TABLE IF EXISTS Temperature; CREATE TABLE Temperature ( ID_Card VARCHAR(30) NOT NULL COMMENT '身份證號', Time DATETIME NOT NULL COMMENT '測量時間', Temperature FLOAT NOT NULL COMMENT '體溫', Dtype VARCHAR(100) COMMENT '設備類型', Did VARCHAR(100) COMMENT '設備編碼', Medic_Id VARCHAR(100) COMMENT '醫生ID', Note TEXT COMMENT '備註', CONSTRAINT PK_Temperature PRIMARY KEY(ID_Card,Time) ); ALTER TABLE Temperature COMMENT = '體溫';
插入數據:函數
INSERT INTO temperature VALUES (11, '5329321990011', '2016-8-11 12:14:34', 11, NULL, NULL, NULL, NULL); INSERT INTO temperature VALUES (13, '5329321990011', '2016-8-11 15:43:10', 21, NULL, NULL, NULL, NULL); INSERT INTO temperature VALUES (14, '5329321990011', '2016-8-11 15:52:10', 11, NULL, NULL, NULL, NULL); INSERT INTO temperature VALUES (15, '5329321990011', '2016-8-12 12:14:34', 2, NULL, NULL, NULL, NULL); INSERT INTO temperature VALUES (16, '5329321990011', '2016-8-12 13:39:52', 333, NULL, NULL, NULL, NULL); INSERT INTO temperature VALUES (17, '5329321990011', '2016-8-12 16:48:37', 1.2, NULL, NULL, NULL, NULL); INSERT INTO temperature VALUES (21, '5329321990011', '2016-8-12 17:58:39', 42, NULL, NULL, NULL, NULL); INSERT INTO temperature VALUES (24, '5329321990011', '2016-8-19 09:51:12', 100, NULL, NULL, NULL, NULL); INSERT INTO temperature VALUES (24, '5329321990011', '2016-8-19 09:51:32', 101, NULL, NULL, NULL, NULL); INSERT INTO temperature VALUES (24, '5329321990011', '2016-8-19 09:52:12', 103, NULL, NULL, NULL, NULL); INSERT INTO temperature VALUES (24, '5329321990011', '2016-8-19 09:53:12', 107, NULL, NULL, NULL, NULL); INSERT INTO temperature VALUES (25, '5301211998012', '2016-8-18 17:34:37', 55, NULL, NULL, NULL, NULL); INSERT INTO temperature VALUES (26, '5329011995112', '2016-8-16 10:17:19', 12313, NULL, NULL, NULL, NULL); INSERT INTO temperature VALUES (27, '5329011995112', '2016-8-18 11:47:24', 22, NULL, NULL, NULL, NULL);
需求:查詢出8月裏天天最新的一條數據 一、第一步先降序:編碼
SELECT * FROM temperature ORDER BY Time DESC
二、第二步:排序後分組,GROUP BY time, 用substr()函數截取時間後格式爲yyyyy-mm-dd,這樣就變成了按天進行分組,GROUP BY後默認顯示每組中的第一條數據。以下code
SELECT * FROM (SELECT * FROM temperature ORDER BY Time DESC) t1 WHERE t1.time>='2016-08-01 00:00:00' AND t1.time<'2016-09-01 00:00:00' GROUP BY substr(t1.time, 1, 10)
查詢查來的數據以下: 排序