Friday, 08 March 2013 13:55

how to join two tables by Left Join sql

Written by 
Rate this item
(0 votes)

there are different kind of joins available by sql like
inner join,left join,etc.by the following example i will show
how to do left join which i used in my application to
get data from two tables -

using (connection)
{
connection.Open();
cmd = connection.CreateCommand();
cmd.CommandText = "SELECT Project.ProjectId,Project.ProjectName,BidTable.BidId
FROM (BidTable LEFT JOIN Project ON BidTable.ProjectId=Project.ProjectId)
WHERE BidTable.SubContractorId=@sid ORDER BY BidDate DESC";

cmd.Parameters.Add("@sid", SqlDbType.NVarChar, -1);
cmd.Parameters["@sid"].Value = IDNumber.ToString();
reader = cmd.ExecuteReader();
while (reader.Read())
{
//get data here
}
}

by the left join i am getting all records from BidTable whether there
is a match or not with Project table.

Read 1358 times
Super User

Email This email address is being protected from spambots. You need JavaScript enabled to view it.
Login to post comments