简体   繁体   中英

Complex query, many joins

I've been wrestling with this for a few hours and I'm hoping you can give me some fresh insight. I have 6 tables as follows:

  • Table A
  • Table B, which is a child of A (one-to-many)
  • Table C, which is a child of B (one-to-many)
  • Table D, which is a another child of A (one-to-many)

  • Table E is another parent of D, in a one-to-[zero-or-one] relationship

  • Table F, which is another child of E (one-to-many)

Basically I need to select a field from B where C = F.

I have tried with subqueries, joins, and a combination of both, but have not got too far. Any ideas would be appreciated.

With the information you've presented, how about

SELECT  *
FROM    A
        INNER JOIN B ON B.AID = A.AID
        INNER JOIN C ON C.BID = B.BID
        INNER JOIN D ON D.AID = A.AID
        INNER JOIN E ON E.DID = D.DID
        INNER JOIN F ON F.EID = E.EID
WHERE   C.Field = F.Field

If this is not what you need, you might want to post a small subset of data with the required results.

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-2025 STACKOOM.COM