Skip to content

Instantly share code, notes, and snippets.

@tom--
Last active May 20, 2017 21:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tom--/893e62688b69014e09ba1602c8dc52bf to your computer and use it in GitHub Desktop.
Save tom--/893e62688b69014e09ba1602c8dc52bf to your computer and use it in GitHub Desktop.
Is a STRAIGHT_JOIN like a LEFT JOIN or like an INNER JOIN? (MySQL)
create table t (
id int primary key,
r_id int,
c tinyint(1) default 0
);
create table r (
id int primary key,
c tinyint(1) default 0
);
insert into r values
(1, 0),
(2, 0),
(3, 0),
(4, 0),
(5, 1),
(6, 1),
(7, 1),
(8, 1);
insert into t (id, r_id) values
(1, 1),
(2, 2),
(3, NULL),
(4, 74),
(5, 5),
(6, 6),
(7, NULL),
(8, 75);
select t.id, r.c from t
left join r on r.id = t.r_id
where r.c = 1 OR r.id IS NULL;
+----+------+
| id | c |
+----+------+
| 3 | NULL |
| 4 | NULL |
| 5 | 1 |
| 6 | 1 |
| 7 | NULL |
| 8 | NULL |
+----+------+
select t.id, r.c from t
inner join r on r.id = t.r_id
where r.c = 1 OR r.id IS NULL;
+----+------+
| id | c |
+----+------+
| 5 | 1 |
| 6 | 1 |
+----+------+
select t.id, r.c from t
straight_join r on r.id = t.r_id
where r.c = 1 OR r.id IS NULL;
+----+------+
| id | c |
+----+------+
| 5 | 1 |
| 6 | 1 |
+----+------+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment