Skip to content

Instantly share code, notes, and snippets.

@xhackjp1
Created February 6, 2019 04:55
Show Gist options
  • Save xhackjp1/efbc31d6224110db99f5c789d8d05f91 to your computer and use it in GitHub Desktop.
Save xhackjp1/efbc31d6224110db99f5c789d8d05f91 to your computer and use it in GitHub Desktop.
サンプルSQL
-- 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;
-- 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