Skip to content

Instantly share code, notes, and snippets.

@supervoron1
Created February 11, 2020 18:22
Show Gist options
  • Save supervoron1/0594c46d30008b0864b0e222fbb0d826 to your computer and use it in GitHub Desktop.
Save supervoron1/0594c46d30008b0864b0e222fbb0d826 to your computer and use it in GitHub Desktop.
SQL Join as many as you like
Select <column list>
From T1
Join T2 on T1.key = T2.key
Join T3 in T2.key = T3.key
---------------------------------
CREATE TABLE `authors` (
`id` int(11) NOT NULL,
`title` varchar(32) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
CREATE TABLE `status` (
`id` int(11) NOT NULL,
`title` varchar(32) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
CREATE TABLE `tasks` (
`id` int(11) NOT NULL,
`title` varchar(64) NOT NULL,
`author_id` int(11) NOT NULL,
`status_id` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
-------------------------- *** --------------------------
SELECT tasks.id, tasks.title, authors.title AS author_name, status.title AS status_name
FROM tasks
JOIN authors ON tasks.author_id = authors.id
JOIN status ON tasks.status_id = status.id
В результате объединение 3 таблиц по `author_id` и `status_id`
--------------------------- *** -----------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment