title: Writing SQL statement to select from multiple rows
body: <p>This might be a silly question to ask. I will appreciate any kind of help.</p>
  
  <p>I'm trying to write a query that count the number of rows based on the logic as below:</p>
  
  <p>count no rows where username = @username and friendsname = @friendsname and username = @friendsname and @friendsname = @username where status = 0</p>
  
  <p>consider the table in the diagram below:</p>
  
  <p><a href="https://i.stack.imgur.com/nOly0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nOly0.png" alt="enter image description here"></a> </p>
  
  <p>The query should return 1 since id '18' and id '20' have reciprocal values. How will this query be written?</p>
  
  <p>I have written the query as follows which doesn't work:</p>
  
  <pre><code>        bool flag = false;
      StringBuilder query = new StringBuilder();
      query.Append("SELECT Count(id) from friends where username=@username AND friend_username=@friend_username AND username = @friend_username AND friend_username = @username");
      query.Append("  AND status=0");
  
      using (SqlConnection con = new SqlConnection(Config.ConnectionString))
      {
          con.Open();
  
          using (SqlCommand cmd = new SqlCommand(query.ToString(), con))
          {
              cmd.CommandType = CommandType.Text;
              cmd.Parameters.Add(new SqlParameter("@username", username));
              cmd.Parameters.Add(new SqlParameter("@friend_username", friendusername));
              if (Convert.ToInt32(cmd.ExecuteScalar()) &gt; 0)
                  flag = true;
          }
      }
  
      return flag;
  </code></pre>