Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
サンプル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
You can’t perform that action at this time.