简体   繁体   中英

SQL Inner join in MS Access 2007

I am facing problem with Inner join in Access Database. I have two table having two column each. one is MID and other is DOB in both table. The name of first table is maintable and other is under5. I want to retrieve all those records that are present in under5 table after matching it with maintable so i used Inner join. the problem is there is a MID 10106 whose frequency in under5 table is 2 and in maintable it is 5 but after running the query the number of MID(10106) returned is 10( i guess the expected behaviour should be 2) which is really absurd. please help me to solve this problem. This is my Query:

SELECT maintable.MID, maintable.DOB, Under5.MID, Under5.DOB
FROM under5
INNER JOIN maintable ON under5.MID=maintable.MID;

The behaviour is abosolutely fine.

Every row in Main Table will be matched with Every Row in inner table. As there are 5 rows in Main Table And two rows in inner table 5*2 = 10 rows

Can there be any other way so that i could only retrieve those MID and DOB 
that is present in under5 table after matching it with maintable.

try this

SELECT distinct Under5.MID, Under5.DOB 
FROM under5, MainTable
where under5.mid = mainTable.mid

You are describing a semijoin eg try:

SELECT Under5.MID, Under5.DOB
  FROM under5
 WHERE EXISTS (
               SELECT * 
                 FROM maintable 
                WHERE maintable.MID = under5.MID
              );

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