如何显示每位经理下的所有员工

时间:2017-02-08 07:07:45

标签: c# sql

我有两张桌子:

  

Mrg :UserID || UserID_Mrg ||状态

  

用户:用户ID ||姓名

我想在每个Manager(UserID_Mrg)下显示所有员工(UserID)

示例:

UserID_Mrg = 12

因此,显示UserID_Mrg = 12

的所有UserID

到目前为止,我只能向用户显示用户信息:

C#:

private void BindGridView()
{
    using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString))
        using (SqlDataAdapter adapter = new SqlDataAdapter(
                     @"SELECT tbl_user.Name, tbl_mrg.UserID_Mrg
                       FROM   tbl_mrg 
                       INNER JOIN tbl_user ON tbl_mrg.UserID = tbl_user.UserID 
                       WHERE  ((tbl_mag.user_ID = @UserID))
                       ORDER BY     tbl_user.Name", con))
        {
            adapter.SelectCommand.Parameters.Add(new SqlParameter("@UserID", SessionHelper.UserID));
            adapter.Fill(Data2);
            GridView2.DataSource = Data2;
            GridView2.DataBind();
        }
}

ASPX:

<asp:GridView ID="GridView2" runat="server">
    <Columns>      
        <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
        <asp:BoundField DataField="Mrg" HeaderText="Manager" SortExpression="Manager" />
    </Columns>
</asp:GridView>

1 个答案:

答案 0 :(得分:2)

只需更改where条件

SELECT tbl_user.Name As EmployeeName,tbl_user.UserID  as EmployeeId tbl_mrg.UserID_Mrg as ManagerId
         FROM   tbl_mrg 
         INNER JOIN tbl_user ON tbl_mrg.UserID = tbl_user.UserID 
         WHERE  ((tbl_mag.UserID_Mrg = @UserID)

由于您希望获得管理员下的所有用户,因此内部联接很好,并且在条件将为您提供所需结果的位置有所改变