简体   繁体   English

在条件下使用 DATE() 时,LEFT JOIN 像 INNER 一样工作

[英]LEFT JOIN is working like INNER when using DATE() in condition

I have the following sample data, query and result:我有以下示例数据、查询和结果:

DROP TABLE IF EXISTS calendar;

CREATE TABLE `calendar`  (
  `day_date` date NOT NULL,
  PRIMARY KEY (`day_date`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

DROP TABLE IF EXISTS finance;

CREATE TABLE `finance`  (
  `finance_id` int(11) NOT NULL,
  `timestamp` datetime(0) NOT NULL,
  `debit` int(11) NULL DEFAULT NULL,
  `credit` int(11) NULL DEFAULT NULL,
  PRIMARY KEY (`finance_id`) USING BTREE,
  UNIQUE INDEX `finance_id`(`finance_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Dynamic;


insert into `calendar` values 
('2020-01-01'),
('2020-01-02'),
('2020-01-03'),
('2020-01-04'),
('2020-01-05');

insert into finance values
(1, '2020-01-02 00:00:00', 100, 0),
(2, '2020-01-04 9:00:00', 200, 0);


select day_date , finance.`timestamp`, debit 
from calendar
left join finance on calendar.day_date = date(finance.`timestamp`);

+------------+---------------------+-------+
| day_date   | timestamp           | debit |
+------------+---------------------+-------+
| 2020-01-02 | 2020-01-02 00:00:00 |   100 |
| 2020-01-04 | 2020-01-04 09:00:00 |   200 |
| 2020-01-01 | NULL                |  NULL |
| 2020-01-03 | NULL                |  NULL |
| 2020-01-05 | NULL                |  NULL |
+------------+---------------------+-------+

Where day_date is date, while timestamp is datetime.其中day_date是日期,而timestamp是日期时间。

The problem is that when I use date() in the on clause, the query only returns matching records in both tables, while it should get ALL records from calendar, along with any matching records from finance.问题是当我在 on 子句中使用 date() 时,查询只返回两个表中的匹配记录,而它应该从日历中获取所有记录,以及财务中的任何匹配记录。

However, when I remove date() function from the on clause, it works as it should.但是,当我从 on 子句中删除 date() function 时,它可以正常工作。 Any idea why it's behaving like this?知道为什么它会这样吗?

Update: I tried the query in dbfiddle here: https://www.db-fiddle.com/f/bVvbUy1rirBp5tD9Fq9J8X/0 and it seems it's working fine there.更新:我在这里尝试了 dbfiddle 中的查询: https://www.db-fiddle.com/f/bVvbUy1rirBp5tD9Fq9J8X/0似乎在那里工作正常。 The problem only appears in my environment (MySql v5.7).该问题仅出现在我的环境中(MySql v5.7)。

Your first datetime for the date field '2020-01-02' is '2020-01-02 00:00:00'日期字段“2020-01-02”的第一个日期时间是“2020-01-02 00:00:00”

When you compapre a date with a datetime you get the comparison of '2020-01-02 00:00:00' vs '2020-01-02 00:00:00' and that is equal当您将日期与日期时间进行比较时,您会得到“2020-01-02 00:00:00”“2020-01-02 00:00:00”的比较,这是相等

But for the 4th但是对于第四

you have '2020-01-04 9:00:00' but the date to compare is '2020-01-04 00:00:00', because a date adds the 00:00:00 automatically for comparison and is not equal您有“2020-01-04 9:00:00” ,但要比较的日期是“2020-01-04 00:00:00”,因为日期会自动添加 00:00:00 进行比较并且不相等

but when you make a date('2020-01-04 9:00:00') you receive '2020-01-04' and that is again equal to the date field '2020-01-04'但是当您创建一个日期('2020-01-04 9:00:00')时,您会收到“2020-01-04” ,这再次等于日期字段“2020-01-04”

So the difference are the hours that are compare in datetime they are relevant in date not所以区别在于日期时间比较的小时数,它们与日期不相关

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM