Last active
October 6, 2021 02:19
-
-
Save mmyoji/e95a2f2dd355b7b2cf39960da7397641 to your computer and use it in GitHub Desktop.
[SQLime] Check INNER JOIN
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT * | |
FROM orgs | |
INNER JOIN users | |
ON users.org_id = orgs.id | |
AND users.active = true | |
INNER JOIN users_tasks | |
ON users_tasks.user_id = users.id | |
AND users_tasks.status = "active" | |
INNER JOIN tasks | |
ON tasks.id = users_tasks.task_id | |
AND tasks.code = "run" | |
WHERE | |
orgs.name = "org-A" | |
; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
BEGIN TRANSACTION; | |
PRAGMA writable_schema=ON; | |
CREATE TABLE IF NOT EXISTS orgs ( | |
id integer primary key, | |
name varchar(255) NOT NULL | |
); | |
CREATE TABLE IF NOT EXISTS users ( | |
id integer primary key, | |
org_id integer NOT NULL, | |
name varchar(255) NOT NULL, | |
active boolean NOT NULL | |
); | |
CREATE TABLE IF NOT EXISTS tasks ( | |
id integer primary key, | |
code varchar(20) NOT NULL | |
); | |
CREATE TABLE IF NOT EXISTS users_tasks ( | |
id integer primary key, | |
user_id integer NOT NULL, | |
task_id integer NOT NULL, | |
status varchar(50) NOT NULL | |
); | |
INSERT INTO "orgs" VALUES(1, "org-A"); | |
INSERT INTO "orgs" VALUES(2, "org-B"); | |
INSERT INTO "users" VALUES(1, 1, "user-1-A", true); | |
INSERT INTO "users" VALUES(2, 1, "user-1-B", true); | |
INSERT INTO "users" VALUES(3, 1, "user-1-C", false); | |
INSERT INTO "users" VALUES(4, 2, "user-2-A", true); | |
INSERT INTO "users" VALUES(5, 2, "user-2-B", true); | |
INSERT INTO "users" VALUES(6, 2, "user-2-C", false); | |
INSERT INTO "tasks" VALUES(1, "run"); | |
INSERT INTO "tasks" VALUES(2, "walk"); | |
INSERT INTO "users_tasks" VALUES(1, 1, 1, "active"); | |
INSERT INTO "users_tasks" VALUES(2, 1, 2, "active"); | |
INSERT INTO "users_tasks" VALUES(3, 2, 1, "active"); | |
INSERT INTO "users_tasks" VALUES(4, 2, 2, "inactive"); | |
INSERT INTO "users_tasks" VALUES(5, 3, 1, "active"); | |
INSERT INTO "users_tasks" VALUES(6, 4, 1, "active"); | |
INSERT INTO "users_tasks" VALUES(7, 5, 2, "active"); | |
INSERT INTO "users_tasks" VALUES(8, 6, 1, "inactive"); | |
PRAGMA writable_schema=OFF; | |
COMMIT; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment