Nitish Ranjan
Nitish Ranjan

Reputation: 45

Syntax for inner Join query for 4 table

I want to get the data from my access database and show the data at Datagridview. my tables are:

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

The query which I am using to retrieve the data in C# is:

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
}

The error which I am getting is:

Syntax error at JOIN expression

I want TName, Tprice, FCp(TABLE 4), Fprice as my output. Am I doing it right or is there any other way to do that.

Upvotes: 1

Views: 144

Answers (1)

Gilad Green
Gilad Green

Reputation: 37299

In your from you have opening parentheses with no closing (the first one).

Besides fixing the parentheses it is much more readable if you jump lines:

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)

Upvotes: 4

Related Questions