嵌套子查询可从两个以上的表中进行选择

时间:2013-03-31 01:20:15

标签: c# sql data-binding datagridview oracle10g

我有三张桌子:

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

我想将DataGridView填入:

Client Name, Amount, Year, Month 

我使用了这个陈述,但它没有用:

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)

1 个答案:

答案 0 :(得分:2)

听起来你只是想使用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

请参阅下文,了解JOINs的良好直观表示:

Visual representation of JOINS