I'm wondering why does the following queries give me a slightly different dataset:
SELECT t.name, COUNT(e.id)
FROM event_type t
LEFT JOIN event e ON t.id = e.type_id AND e.start BETWEEN ? AND ?
GROUP BY t.name;
SELECT t.name, COUNT(e.id)
FROM event_type t
LEFT JOIN event e ON t.id = e.type_id
WHERE e.start BETWEEN ? AND ?
GROUP BY t.name;
So I just moved BETWEEN clause to the main body, logically, it does not matter where to apply it, but the result says it matters. Any suggestions? Thanks!
UPD: tried on MySQL 5.6
create table event_type
(
id int auto_increment primary key,
name varchar(100) not null,
constraint UNIQ_93151B825E237E06 unique (name)
) collate = utf8_unicode_ci;
create table event
(
id int auto_increment primary key,
type_id int null,
start datetime not null,
...
constraint FK_3BAE0AA7C54C8C93
foreign key (type_id) references event_type (id)
) collate = utf8_unicode_ci;
create index IDX_3BAE0AA7C54C8C93
on event (type_id);
Maybe it's hard to answer this question without some images! but I try.
Let's assume this is the
event_typetableEventstable:So for this query:
The result will be:
But why? becuase sql engine when try to get result on left join, it will check both of
idandstart, actually the result of prevois query is like this:That's it! When you try to use
Betweeninwhereclause, in fact you are filtering the null values so sql would ingore them and the final result would be different.I hope it's clear enough!