【发布时间】:2018-05-21 23:29:02
【问题描述】:
我有两个具有以下架构的数据库:
tbl_users(userID, fullName, emailID, password)
userID 是主键
tbl_savings(savingsID, creditorName, amount, interestRate, interestType, interestCalculationMethod, ownerID, dataEnteredByID)
savingsID 是主键。
ownerID 是具有一对多关系的 tbl_users userID 上的外键。
ownerID 指定保存所有者的 ID。同样,dataEnteredByID 指定输入数据的人的 ID。
我有一个具有以下规范的 GridView:
<asp:GridView ID="GridViewSavingsTracker" AutoGenerateColumns="false" runat="server" DataKeyNames="savingsID" OnRowCommand="GridViewSavingsTracker_RowCommand" AllowPaging="true" OnSelectedIndexChanged="GridViewSavingsTracker_SelectedIndexChanged" OnPageIndexChanging="GridViewSavingsTracker_PageIndexChanging" PageSize="10">
<Columns>
<asp:CommandField ShowSelectButton="true" />
<asp:BoundField DataField="creditorName" HeaderText="Creditor Name" />
<asp:BoundField DataField="amount" HeaderText="Principal Amount" />
<asp:BoundField DataField="interestRate" HeaderText="Interest Rate" />
<asp:BoundField DataField="interestType" HeaderText="Interest Type" />
<asp:BoundField DataField="interestCalculationMethod" HeaderText="Interest Calculation Method" />
<asp:BoundField DataField="insertedDate" HeaderText="Date" />
<asp:BoundField DataField="ownerID" HeaderText="Owner" />
<asp:BoundField DataField="dataEnteredByID" HeaderText="Data Entered By" />
</Columns>
</asp:GridView>
我想要做什么:我不想打印 ownerID 和 dataEnteredByID,而是想打印相应 ID 的 fullNames。我该怎么做?
我已经拥有的:我有以下用于 GridView 的服务器端代码。我有一个方法 ShowGridView() 将数据从数据库填充到 GridView 中。
protected void ShowGridView()
{
string connectionString = GetConnString();
OleDbConnection con = new OleDbConnection(connectionString);
con.Open();
string showGridViewQuery = "(SELECT s.creditorName, s.amount, s.interestRate, s.interestType, s.interestCalculationMethod, s.insertedDate, u.fullName FROM tbl_savings s LEFT JOIN tbl_users u ON s.ownerID = u.usersID) UNION (select s.creditorName, s.amount, s.interestRate, s.interestType, s.interestCalculationMethod, s.insertedDate, u.fullName from tbl_savings s LEFT JOIN tbl_users u ON s.dataEnteredByID = u.usersID)";
OleDbCommand showGridViewCommand = new OleDbCommand(showGridViewQuery, con);
showGridViewCommand.ExecuteNonQuery();
DataTable dt = new DataTable();
OleDbDataAdapter olda = new OleDbDataAdapter(showGridViewCommand);
olda.Fill(dt);
GridViewSavingsTracker.DataSource = dt;
GridViewSavingsTracker.DataBind();
con.Close();
}
【问题讨论】:
-
您不需要
UNION。JOIN储值表与你的用户表两次,一次获取所有者的姓名,一次获取输入者的姓名。 -
SELF JOIN不会解决我的问题,因为我不是在寻找具有相同 ID 或同名的人。我已经在输入这两列的 ID。我只想打印相应的全名,而不是打印 ID。 -
我没有谈论自我加入。再读一遍。
-
它说找不到页面
-
抱歉,这是私人的。再次,这次公开:rextester.com/YCYA27221
标签: sql asp.net webforms oledb