内联接查询4表的语法

时间:2017-07-30 08:01:42

标签: c# sql ms-access

我想从我的访问数据库中获取数据并在Datagridview显示数据。我的表是:

TABLE 1(AB)           TABLE 2(CD)         TABLE 3(EF)      TABLE 4(GH)
-------------        -----------------   -------------    ------------------- 
SID SName CID TID    TID TName Tprice     FID CID FCp      FPID FID FCp Fprice

我用来在C#中检索数据的查询是:

OleDbCommand command1 = new OleDbCommand();
command1.Connection = connection;
command1.CommandText = "select T.TName, T.Tprice, P.FCp, P.Fprice from (([AB] S inner join [CD] T on S.TID = T.TID) (inner join [EF] C on S.CID = C.CID) inner join [GH] P on C.FID = P.FID where (S.SID = 2) ";
OleDbDataReader myreader = command1.ExecuteReader();
while (myreader.Read())
{
    //DATA IS READ HERE
}

我得到的错误是:

  

JOIN表达式的语法错误

我希望TName, Tprice, FCp(TABLE 4), Fprice作为输出。我做得对吗还是有其他方法可以做到这一点。

1 个答案:

答案 0 :(得分:4)

在你的from中,你有一个没有结束的开括号(第一个)。

除了修复括号外,如果你跳线,它会更具可读性:

command1.CommandText = @"select T.TName, T.Tprice, P.FCp, P.Fprice 
                         from (([AB] S 
                         inner join [CD] T on S.TID = T.TID)
                         inner join [EF] C on S.CID = C.CID) 
                         inner join [GH] P on C.FID = P.FID
                         where S.SID = 2";

(Access requires parentheseses)