Bakuard Asked:2020-06-10 17:01:42 +0000 UTC2020-06-10 17:01:42 +0000 UTC 2020-06-10 17:01:42 +0000 UTC SQL。多对多的关系。查找具有相同链接的所有记录 772 再会。 有两个表:TableA 和 TableB。这些表之间存在多对多的关系(这种关系存储在 TableAtoB(idA, idB) 表中)。如何在 TableB 中找到与 TableA 中的给定记录相关但与 TableA 中的任何其他记录无关的所有记录?TableA 中的一条记录由 id 指定。 sql 4 个回答 Voted CrazyElf 2020-06-10T17:22:07Z2020-06-10T17:22:07Z 实际上,没有必要参考 TableA 表: select * from TableB left join TableAtoB on TableAtoB.idB = TableB.id where TableAtoB.idA = <этот ваш id> Miron 2020-06-10T17:22:42Z2020-06-10T17:22:42Z SELECT TableB.* FROM TableB JOIN TableAToB ON TableAToB.idA = yourAID AND TableB.id = TableAToB.idB; Best Answer Bakuard 2020-06-17T16:26:01Z2020-06-17T16:26:01Z 只能在“额头”上找到解决方案 SELECT TableAToB.idB FROM TableAToB WHERE TableAToB.idA = ? AND TableAToB.idB IN (SELECT TableAToB.idB FROM TableAToB GROUP BY TableAToB.idB HAVING COUNT(TableAToB.idB) = 1); Novitskiy Denis 2020-06-17T16:55:52Z2020-06-17T16:55:52Z 变体exists select * from AtoB ab where Aid=1 and not exists (select * from AtoB ab2 where ab2.Aid<>1 and ab2.bid=ab.Bid) 在这里提琴
实际上,没有必要参考 TableA 表:
只能在“额头”上找到解决方案
变体
exists在这里提琴