简体   繁体   中英

Nested sub-query to select from more than tow table

I have three tables:

Years (YearId,Year)
Months (MonthID,Month)
Date (DateID,YearID,MonthID)
Client (CLientID,ClientName)
Payment (PayID,ClientID,DateID,Amount)

I want to fill in DataGridView as:

Client Name, Amount, Year, Month 

I used this statement but it didn't work:

Select * From (select Year From Years Where YearID = (Select YearId From Date Where dateID = (Select DateID From Payment))),(Select Month From Months Where Monthid = (Select MonthID From Date Where dateID = (Select DateID From Payment))),(Select ClientName From Client Where ClientID = (Select ClientID From Payment),(Select amont From Payment)

Sounds like you're just looking to use INNER JOINs :

SELECT C.ClientName,
    P.Amount,
    Y.Year,
    M.Month
FROM Payment P
    INNER JOIN Client C 
        ON P.ClientID = C.ClientID
    INNER JOIN Date D 
        ON P.DateId = D.DateId
    INNER JOIN Years Y
        ON D.YearId = Y.YearId
    INNER JOIN Months M
        ON D.MonthId = M.MonthId

See below for a good visual representation of JOINs :

Visual representation of JOINS

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