从与另一个 table 有 n:n 关系的 table 中选择,并在第二个 table 中检查了参数
selecting from a table that got an n:n relation to another table with an argument checked in the second table
我得到了类似下面的 Table 结构
table a
Int id primary key
String description
table b
Int id primary key
some fields...
table ab (as a linking table because the table a and b have a n:n relation)
Int id primary key
int bid foreign key to b.id
int aid foreign key to a.id
我如何 select 每个 b
链接到 a
的描述是 'Test'
?
现在我正在使用这种方法(真的很慢)
首先我select一个with
的id
select id from a
where description = 'Test';
然后我将 table ab 中链接的所有来自 b 的 ID 放入列表
select bid from ab
where aid = id;
最后我select他们在这个查询的for循环中一个一个地
select * from b
where id = id;
那么我该如何改进呢?
谢谢你的帮助
请用您正在使用的 sql 提供商标记您的问题。
无论如何,像这样:
select b.*
from a join ab on a.id = ab.aid join b on ab.bid = b.id
where a.description = 'TEST';
我得到了类似下面的 Table 结构
table a
Int id primary key
String description
table b
Int id primary key
some fields...
table ab (as a linking table because the table a and b have a n:n relation)
Int id primary key
int bid foreign key to b.id
int aid foreign key to a.id
我如何 select 每个 b
链接到 a
的描述是 'Test'
?
现在我正在使用这种方法(真的很慢)
首先我select一个with
的idselect id from a
where description = 'Test';
然后我将 table ab 中链接的所有来自 b 的 ID 放入列表
select bid from ab
where aid = id;
最后我select他们在这个查询的for循环中一个一个地
select * from b
where id = id;
那么我该如何改进呢?
谢谢你的帮助
请用您正在使用的 sql 提供商标记您的问题。
无论如何,像这样:
select b.*
from a join ab on a.id = ab.aid join b on ab.bid = b.id
where a.description = 'TEST';