可能有一个更有效的解决方案,但这样的事情应该可以工作......
DROP TABLE IF EXISTS ph;
CREATE TABLE Ph(time datetime PRIMARY KEY, Ph DECIMAL(5,2) NOT NULL);
DROP TABLE IF EXISTS tss;
CREATE TABLE tss(time datetime PRIMARY KEY,solids int NOT NULL);
DROP TABLE IF EXISTS temperature;
CREATE TABLE temperature(time datetime NOT NULL PRIMARY KEY,temp DECIMAL(5,2) NOT NULL);
INSERT INTO temperature VALUES
('2013-03-29 21:34:05',26.56),
('2013-03-29 21:35:05',26.56),
('2013-03-29 21:36:05',26.56);
INSERT INTO tss VALUES
('2013-03-29 21:34:19',146),
('2013-03-29 21:35:19',146),
('2013-03-29 21:36:19',146);
INSERT INTO ph VALUES
('2013-03-29 21:34:04',7.39),
('2013-03-29 21:35:04',7.43),
('2013-03-29 21:36:04',7.43);
SELECT *
FROM ph
JOIN tss
ON DATE_FORMAT(tss.time,'%Y%m%d%h%i') = DATE_FORMAT(ph.time,'%Y%m%d%h%i')
JOIN temperature t
ON DATE_FORMAT(t.time,'%Y%m%d%h%i') = DATE_FORMAT(ph.time,'%Y%m%d%h%i');
+---------------------+------+---------------------+--------+---------------------+-------+
| time | Ph | time | solids | time | temp |
+---------------------+------+---------------------+--------+---------------------+-------+
| 2013-03-29 21:34:04 | 7.39 | 2013-03-29 21:34:19 | 146 | 2013-03-29 21:34:05 | 26.56 |
| 2013-03-29 21:35:04 | 7.43 | 2013-03-29 21:35:19 | 146 | 2013-03-29 21:35:05 | 26.56 |
| 2013-03-29 21:36:04 | 7.43 | 2013-03-29 21:36:19 | 146 | 2013-03-29 21:36:05 | 26.56 |
+---------------------+------+---------------------+--------+---------------------+-------+
CREATE TABLE normalised AS
SELECT ph.time
, ph.ph
, tss.solids
, t.temp
FROM ph
JOIN tss
ON DATE_FORMAT(tss.time,'%Y%m%d%h%i') = DATE_FORMAT(ph.time,'%Y%m%d%h%i')
JOIN temperature t
ON DATE_FORMAT(t.time,'%Y%m%d%h%i') = DATE_FORMAT(ph.time,'%Y%m%d%h%i');
SELECT * FROM normalised;
+---------------------+------+--------+-------+
| time | ph | solids | temp |
+---------------------+------+--------+-------+
| 2013-03-29 21:34:04 | 7.39 | 146 | 26.56 |
| 2013-03-29 21:35:04 | 7.43 | 146 | 26.56 |
| 2013-03-29 21:36:04 | 7.43 | 146 | 26.56 |
+---------------------+------+--------+-------+
ALTER TABLE normalised ADD PRIMARY KEY(time);