mysql:无法根据最接近的时间戳创建连接

时间:2018-03-31 19:57:44

标签: mysql jointable closest sql-timestamp

我在这个伟大的社区中阅读了多个主题,但不知怎的,我无法解决我的问题。

我有两张桌子:

表A.

id    timestamp               value
1     2018-03-30 00:01:00     100
2     2018-03-30 00:02:02     200
3     2018-03-30 00:03:01     300
4     2018-03-30 00:03:59     400
5     2018-03-30 00:05:00     500
6     2018-03-30 00:06:07     600
7     2018-03-30 00:06:54     700
8     2018-03-30 00:08:00     800

表B:

id    timestamp               value
1     2018-03-30 00:00:59     10
2     2018-03-30 00:01:12     20
3     2018-03-30 00:01:20     30
4     2018-03-30 00:01:25     40
5     2018-03-30 00:01:40     50
6     2018-03-30 00:02:01     60
7     2018-03-30 00:02:05     70
8     2018-03-30 00:02:09     80

我试图创建一个语句来加入我的表。并用值计算。

目标表:

timestamp               a.value    b.value   diff
2018-03-30 00:01:00     100        10        90
2018-03-30 00:02:02     200        60        140

对我而言,是为了获得最合适的时间来进行加入。

目前的方法,基于另一个方面:

SELECT 
a.`id`,
a.`timestamp`,
(SELECT b.`timestamp`
FROM `databaseB`.`tableB` as b
where b.`channel_id`=10 
order by abs(datediff(a.`timestamp`, b.`datetime`)) asc Limit 1) AS newtime,
a.value,
FROM `databaseA`.`tableA` as a;

但结果我在newtime列中只收到一个静态值。它是一个时间戳,但它不是创建连接的正确时间。

你们有人能看出我的错吗?我做错了什么。

Thx和KR 霍尔格

1 个答案:

答案 0 :(得分:0)

这可能适合你。您可能必须在between语句中试验间隔。

SQL Fiddle

MySQL 5.6架构设置

DROP TABLE IF EXISTS `tablea`;

CREATE TABLE IF NOT EXISTS `tablea` (
    `id`            INT(11) UNSIGNED        NOT NULL    AUTO_INCREMENT,
    `timestamp`     TIMESTAMP               NOT NULL    DEFAULT '0000-00-00 00:00:00',
    `value`         INT(11) UNSIGNED        NULL        DEFAULT NULL,
    PRIMARY KEY (`id`),
    KEY `idx_timestamp` (`timestamp`)
) 
    ENGINE=MyISAM 
    AUTO_INCREMENT=1 
    DEFAULT CHARSET=utf8 
    COLLATE=utf8_unicode_ci
    COMMENT '';

INSERT INTO `tablea`
(id,timestamp,value)
VALUES
(1,'2018-03-30 00:01:00',100),
(2,'2018-03-30 00:02:02',200),
(3,'2018-03-30 00:03:01',300),
(4,'2018-03-30 00:03:59',400),
(5,'2018-03-30 00:05:00',500),
(6,'2018-03-30 00:06:07',600),
(7,'2018-03-30 00:06:54',700),
(8,'2018-03-30 00:08:00',800);

CREATE TABLE IF NOT EXISTS `tableb` (
    `id`            INT(11) UNSIGNED        NOT NULL    AUTO_INCREMENT,
    `timestamp`     TIMESTAMP               NOT NULL    DEFAULT '0000-00-00 00:00:00',
    `value`         INT(11) UNSIGNED        NULL        DEFAULT NULL,
    PRIMARY KEY (`id`),
    KEY `idx_timestamp` (`timestamp`)
) 
    ENGINE=MyISAM 
    AUTO_INCREMENT=1 
    DEFAULT CHARSET=utf8 
    COLLATE=utf8_unicode_ci
    COMMENT '';

INSERT INTO `tableb`
(id,timestamp,value)
VALUES
(1,'2018-03-30 00:00:59',10),
(2,'2018-03-30 00:01:12',20),
(3,'2018-03-30 00:01:20',30),
(4,'2018-03-30 00:01:25',40),
(5,'2018-03-30 00:01:40',50),
(6,'2018-03-30 00:02:01',60),
(7,'2018-03-30 00:02:05',70),
(8,'2018-03-30 00:02:09',80);

查询1

select
  a.*,
  c.`id`,
  c.`value`,
  a.`value` - c.`value` as `diff`,
  min(b.`timestamp`) as `btimestamp`,
  min(ABS(a.`timestamp` - b.`timestamp`)) as `timediff`
FROM `tablea` a
JOIN `tableb` b
ON b.`timestamp` between 
  date_sub(a.`timestamp`,INTERVAL '0' second) AND 
  date_add(a.`timestamp`,INTERVAL '20' second)
JOIN `tableb` c
ON b.`timestamp` = c.`timestamp`
GROUP BY a.`id`

<强> Results

| id |            timestamp | value | id | value | diff |           btimestamp | timediff |
|----|----------------------|-------|----|-------|------|----------------------|----------|
|  1 | 2018-03-30T00:01:00Z |   100 |  2 |    20 |   80 | 2018-03-30T00:01:12Z |       12 |
|  2 | 2018-03-30T00:02:02Z |   200 |  7 |    70 |  130 | 2018-03-30T00:02:05Z |        3 |