mysql - 如何在 M :N relationship? 的绑定(bind)表中的一列上构造具有 AND 限制的查询

标签 mysql sql database

我有作者和出版商之间的绑定(bind)表。每个作者可以连接到多个出版商,一个出版商可以连接到多个作者。是 M:N 关系。因此我有绑定(bind)表。当然,也可以有仅与其中一个出版商相关联的作者。

我想选择连接到 Publisher1 并同时连接到 Publisher2 的所有作者。我不想在结果集中有只连接到 Publisher1 或只连接到 Publisher2 的作者。只需 Publisher1 和 Publisher2。

我试过这个查询:

SELECT author_id FROM author_publisher where publisher_id = 1 AND where publisher_id = 2

但是没有用。我得到了空集。

我正在尝试使用相交:

SELECT author_id FROM author_publisher where publisher_id = 1 
Intersect 
Select author_id FROM author_publisher where publisher_id = 2

但是我用的是MySQL,好像不支持这个。

有没有人对如何构造这样的查询有任何建议?

最佳答案

您可以使用不相关的子查询...这不是最高效的方式,但您可以尝试:

SELECT author_id FROM author_publisher WHERE publisher_id = 1
and author_id in (SELECT author_id FROM author_publisher WHERE publisher_id = 2)

关于mysql - 如何在 M :N relationship? 的绑定(bind)表中的一列上构造具有 AND 限制的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32994218/

相关文章:

mysql - RELOAD 权限以刷新主机

MYSQL 查询 : How to UNION two table with ORDER BY

mysql - 转义字符使日期值为空

php - 像google一样的数据库搜索

Android:我应该如何处理数据库访问异常?

mysql - 错误: Error 1215: Cannot add foreign key constraint even when both fields are of the same type

sql - 通过 Oracle 的解释计划查询的总成本

sql - 相关记录的条件计数

android - android应用升级到新版本后如何保留旧数据库?

php - MySQL - 每个客户端一张表或所有客户端的一张表