Created
February 6, 2019 04:55
-
-
Save xhackjp1/efbc31d6224110db99f5c789d8d05f91 to your computer and use it in GitHub Desktop.
サンプルSQL
This file contains 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
-- based on answer https://stackoverflow.com/a/7745635/808921 | |
SELECT a.id, a.name, b.name | |
FROM books as a | |
INNER JOIN authors as b | |
ON a.author_id = b.id; |
This file contains 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
-- borrowed from https://stackoverflow.com/q/7745609/808921 | |
CREATE TABLE IF NOT EXISTS `books` ( | |
`id` int(6) unsigned NOT NULL, | |
`author_id` int(6) unsigned NOT NULL, | |
`name` varchar(200) NOT NULL, | |
PRIMARY KEY (`id`) | |
) DEFAULT CHARSET=utf8; | |
CREATE TABLE IF NOT EXISTS `authors` ( | |
`id` int(6) unsigned NOT NULL, | |
`name` varchar(200) NOT NULL, | |
PRIMARY KEY (`id`) | |
) DEFAULT CHARSET=utf8; | |
ALTER TABLE `books` ADD FOREIGN KEY (author_id) REFERENCES `authors` (`id`); | |
INSERT INTO `authors` (`id`, `name`) VALUES | |
('1', 'akutagawa'), | |
('2', 'natsume souseki'), | |
('3', 'fukuzawa yukichi'); | |
INSERT INTO `books` (`id`, `author_id`, `name`) VALUES | |
('1', '1', 'The earth is flat'), | |
('2', '2', 'One hundred angels can dance on the head of a pin'), | |
('3', '2', 'The earth is flat and rests on a bull\'s horn'), | |
('4', '3', 'The earth is like a ball.'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment