简体   繁体   English

从表B获取所有与表A的多个条目(给定列表)相关的条目

[英]Get all entries from Table B which have a relation to multiple entries (given list) from Table A

I have two tables. 我有两张桌子。 Table A and Table B . A和表B Both are connected with a many-to-many relationship. 两者都有多对多的关系。

Table A: 表A:

ID
---
1
2

Table B: 表B:

ID
---
3
4

Table AB: 表AB:

ID | A_ID | B_ID
----------------
5  | 1    | 4
6  | 1    | 3
7  | 2    | 3

I want to get the list of ID s from table B which have a relation to a list of ID s of table A . 我想从表B获取ID 列表,这些ID与表AID列表有关

Example from the above tables: 上表中的示例:

I want to get all B s which have a relation to table A ID 1 and ID 2. I get then ID 3 has to both ID s of table A . 我想获取所有与表A ID 1和ID 2有关系的B然后,我得到ID 3与表A两个ID都有关系。

How could I do this with an SQL query ? 如何使用SQL查询做到这一点?

If you are looking to select based on a list of As (not ALL As), then do it like this: 如果要基于“不作为”列表(而不是“全部”作为)进行选择,请按照以下步骤操作:

SELECT b_id
FROM ab
WHERE a_id IN (1,2)
GROUP BY b_id
HAVING COUNT(a_id) = 2

Replace (1,2) with your list and 2 in the having clause with the number of list items. (1,2)替换为您的列表,将hading子句中的2替换为列表项的数量。

If you get your list of As from a subquery you could do it like that (not in MySQL, though...): 如果从子查询中获得As的列表,则可以这样做(尽管在MySQL中不行,但是...):

WITH subquery (
 --subquery code here
)

SELECT b_id
FROM ab
WHERE a_id IN subquery
GROUP BY b_id
HAVING COUNT(a_id) = (SELECT COUNT(*) FROM subquery)

In MySQL you would have to put your subquery code twice and drop the WITH clause. 在MySQL中,您必须将子查询代码放入两次,并删除WITH子句。

You could also use a temporary table, which would then lead to selecting ALL As from that temporary table and thus Gordon Linoffs answer... 您还可以使用一个临时表,这将导致从该临时表中选择“全部为”,因此Gordon Linoffs会回答...

You can do this by joining and counting: 您可以通过加入并计数来做到这一点:

SELECT B_ID
FROM AB JOIN A 
         ON
     AB.A_ID = A.ID
GROUP BY AB.B_ID
HAVING COUNT(DISTINCT AB.A_ID) = (SELECT COUNT(distinct ID) FROM A);

If you know there are no duplicates in AB or A , you can remove the distinct from the count() . 如果你知道在没有重复ABA ,可以去除distinctcount()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从B表的所有相应行中仅选择A的值都为X的条目? - How to select only entries from A which have only values=X in all respective rows in B table? SQL筛选器条目与另一个表中的所有条目匹配 - SQL filter entries which match all entries from another table 从表B中选择具有表A中多个条件的条目 - Select entries from table B with multiple conditions from table A 使用表B中的所有新条目更新表A并通过B更新A中的现有条目 - Update table A with all new entries from table B AND update existing entries in A via B 从表中选择所有早于3天的条目 - Select all entries from a table which are older then 3 days MySQL PHP从表B中获取与表A中给定列不匹配的条目的最佳方法是什么 - mysql php what is the best way to obtain from table B the entries that don't match a given column in table A 从一个表中获取所有条目以及在另一个表中的这些条目的计数 - Get all entries from one table together with the count of these entries in another table 从表中获取同一用户的多个条目中的最新条目 - Get latest entry from multiple entries for the same user from a table 查找包含给定集合中的ID的所有聚合表条目 - Find all aggregate table entries that have ids contained in a given set PHP Laravel 数据库:从一张表中获取所有条目+更多 - PHP Laravel Database: Get all entries from one table + more
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM