我想从MySQL数据库中的两个表中获取JOIN
数据,这些数据基于相似但不完全相等的datetime
值。示例数据集:
account
+-----+----------+---------------------+
| id | balance | created_at |
+-----+----------+---------------------+
| 492 | 0.171248 | 2013-11-24 14:45:07 |
| 491 | 0.171248 | 2013-11-24 14:40:07 |
+-----+----------+---------------------+
+-----+-----------+---------------------+
| id | rate | created_at |
+-----+-----------+---------------------+
| 267 | 802.94621 | 2013-11-24 14:45:02 |
| 266 | 802.97802 | 2013-11-24 14:40:01 |
+-----+-----------+---------------------+
通常,两个表中都有通常行,其中created_at
值与分钟匹配,但不会下降到秒。
我想基于JOIN
到最近的分钟datetime
两个表。我试过这个,但它不起作用:
SELECT *,
DATE_FORMAT(`account`.`created_at`, '%Y-%m-%d %h:%i') as `joina`,
DATE_FORMAT(`values`.`created_at`, '%Y-%m-%d %h:%i') as `joinv`
FROM `account`
INNER JOIN `values` ON `joina` = `joinv`
答案 0 :(得分:0)
有时在尝试清楚地表达我的问题时,我在写出问题时偶然发现了正确的方法:
SELECT *
FROM `account`
INNER JOIN `values` ON
DATE_FORMAT(`account`.`created_at`, '%Y-%m-%d %h:%i') = DATE_FORMAT(`values`.`created_at`, '%Y-%m-%d %h:%i');