简体   繁体   中英

sql table join get rows form one table only

I have the following A,B with columns c1,c2

A
--
aId

B
--
bId, aId

Lets say A has the folowing rows

aid
1
3
4

and B has the following rows

bId, aId
6, 1
5, 4
10, 1

I need an output of the following

id
1
4

Issue That is, I am trying to get all rows in table A such that A.aId exists for some row in table B for column aId. I have tried using inner join but it gives me to many rows (in the above example it would give me row 1 twice).

该查询似乎获得了所需的结果:

SELECT DISTINCT(a.aid) FROM a INNER JOIN b ON a.aid = b.aid

You can filter A on a subquery on B :

select * from A where aId in (select aId from B)

If the only column you want from A is aID , you already have it in B :

select distinct aId from B

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM