簡體   English   中英

從與另一個表具有n:n關系的表中進行選擇,並在第二個表中選中一個參數

[英]selecting from a table that got an n:n relation to another table with an argument checked in the second 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

如何選擇與描述為'Test'a鏈接的每個b

現在我正在使用這種方法(真的很慢)

首先我選擇一個的ID

select id from a
where description = 'Test';

然后我將表b中鏈接的b的所有id放入列表

select bid from ab
where aid = id;

最后我用這個查詢在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';

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM