int id = 1;
string chain = "(";
SqlDataReader dr = SqlHelper.ExecuteReader(string.Format("SELECT a.Id as x, c.Id as y From Friends b INNER JOIN Users a ON b.SenderId = a.Id INNER JOIN Users c ON b.ReceiverId = c.Id WHERE (c.Id = {0} OR a.Id = {0}) AND State = '{1}'", id, "ok"));
if (dr.HasRows)
while (dr.Read())
if (id == int.Parse(dr["y"].ToString()))
chain += dr["x"].ToString() + ", ";
else
chain += dr["y"].ToString() + ", ";
if (chain.Length > 1)
chain = chain.Substring(0, chain.Length - 2) + ")";
else
chain = "(0)";
// Chain has for example => (2, 3, 4, 5) => which are the Ids for Users's Friends
string str = "SELECT TOP 20 a.*, b.UserName as Sender, c.UserName as Receiver, b.Avatar as SenderPic";
str += " FROM Events a INNER JOIN Users b ON a.SenderId = b.Id INNER JOIN Users c ON a.ReceiverId = c.Id ";
str += "WHERE SenderId IN ";
str += chain;
str += " OR ReceiverId IN";
str += chain;
str += " Order BY Id desc";
dr = SqlHelper.ExecuteReader(str);
chain
considered as the user's friends .
does any one know how to execute this query with JOINS !? Thanks a lot ..