MySQL 查询:选择链接表的行并捕获“更大”的行数据集
我试图弄清楚如何构建满足以下条件的查询。
我有两张桌子。表a
存储作者列表。表b
存储书籍列表。我有一个链接表c
,它将每个作者映射到一本或多本书。当然,一本书可以有多个作者。给定一个作者的名字(让我们取名字=“道格拉斯·亚当斯”),我知道如果我这样做,
SELECT * FROM linktable
INNER JOIN a ON linktable.a_id = a.id
INNER JOIN b ON linktable.p_id = b.id
WHERE a.name = 'Douglas Adams';
我就会得到道格拉斯·亚当斯写的所有书籍。让我们假设,道格拉斯·亚当斯有时有“合著者”。我如何获得它们?
我想要一个看起来像这样的列表:
Douglas Adams, The Hitchhiker's Guide to the Galaxy, maybe more details...
Douglas Adams, Book_2, maybe more details...
Coauthor_1, Book_2, same Details as in "Douglas Adams, Book_2, maybe more details..."
这可行吗?
我创建了 3 个表,它们映射了我想要存储的内容和想要检索的内容。
2 个存储表是:
CREATE TABLE `a` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `name_UNIQUE` (`name`),
KEY `name_INDEX` (`name`),
FULLTEXT KEY `name_FULLTEXT` (`name`)
) ENGINE=MyISAM AUTO_INCREMENT=932723 DEFAULT CHARSET=utf8 COLLATE=utf8_bin
CREATE TABLE `b` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(1000) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`),
FULLTEXT KEY `title_fulltext` (`title`)
) ENGINE=MyISAM AUTO_INCREMENT=1617432
和一个链接上面 2 个表的三分之一表。
CREATE TABLE `linktable` (
`a_id` int(11) NOT NULL,
`b_id` int(11) NOT NULL,
KEY `a_id_INDEX` (`a_id`),
KEY `b_id_INDEX` (`b_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
I am trying to figure out how to build a query, which satisfies the following criteria.
I have two tables. Table a
stores a list of authors. And table b
stores a list of books. I have a linking table c
, which maps every author to one ore more books. Naturally a book can have more then one author. Given a name (let's take name = "Douglas Adams") of an author, I know that if I do
SELECT * FROM linktable
INNER JOIN a ON linktable.a_id = a.id
INNER JOIN b ON linktable.p_id = b.id
WHERE a.name = 'Douglas Adams';
I get all the books which were written by Douglas Adams. Let us assume, Douglas Adams sometimes had "coauthors". How do I get them?
I want a list the somehow looks like this:
Douglas Adams, The Hitchhiker's Guide to the Galaxy, maybe more details...
Douglas Adams, Book_2, maybe more details...
Coauthor_1, Book_2, same Details as in "Douglas Adams, Book_2, maybe more details..."
Is this doable?
I have created 3 tables, which map what I want to store and what I want to retrieve.
The 2 storage tables are:
CREATE TABLE `a` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `name_UNIQUE` (`name`),
KEY `name_INDEX` (`name`),
FULLTEXT KEY `name_FULLTEXT` (`name`)
) ENGINE=MyISAM AUTO_INCREMENT=932723 DEFAULT CHARSET=utf8 COLLATE=utf8_bin
CREATE TABLE `b` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(1000) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`),
FULLTEXT KEY `title_fulltext` (`title`)
) ENGINE=MyISAM AUTO_INCREMENT=1617432
and one third table, which links the 2 tables above.
CREATE TABLE `linktable` (
`a_id` int(11) NOT NULL,
`b_id` int(11) NOT NULL,
KEY `a_id_INDEX` (`a_id`),
KEY `b_id_INDEX` (`b_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这应该有效:
编辑,替代方案:
This should work:
Edit, Alternative: