SQL将一个表中的两个字段连接到另一个表中

时间:2014-07-24 14:22:47

标签: c# asp.net sql-server

这是我的SQL:

SELECT        
    Users.[First Name], Users.[User ID], Users.Login, 
    Users.Surname, Users.Password, Users.[Account Question], 
    Users.[Account Answer], Users.[Region Access], Users.[Account Access], 
    Users.owningsite, Users.Currentsitesetting, Users.createdby, 
    Users.createddate, Users.lastmodby, Users.lastmoddate, Users.active, 
    Sites.Sitename    
FROM
    Users 
INNER JOIN 
    Sites ON Users.owningsite = Sites.SiteID AND Users.Currentsitesetting = Sites.SiteID    
WHERE
    (Users.owningsite = 1)

现在我需要两个包含ID号(Owningsite& currentsitesetting)的绑定字段来连接站点名称并根据用户表单的ID号提供网站名称,而不是显示1,2而是显示站点A,站点B.

SQL是错误还是我在ASPX标记中遗漏了什么?

<asp:BoundField DataField="First_Name" HeaderText="First_Name" 
    SortExpression="First_Name" />
<asp:BoundField DataField="Surname" HeaderText="Surname" 
    SortExpression="Surname" />
<asp:BoundField DataField="Login" HeaderText="Login" SortExpression="Login" />
<asp:BoundField DataField="Password" HeaderText="Password" 
    SortExpression="Password" />
<asp:BoundField DataField="Account_Question" HeaderText="Account_Question" 
    SortExpression="Account_Question" />
<asp:BoundField DataField="Account_Answer" HeaderText="Account_Answer" 
    SortExpression="Account_Answer" />
<asp:BoundField DataField="Region_Access" HeaderText="Region_Access" 
    SortExpression="Region_Access" />
<asp:BoundField DataField="Account_Access" HeaderText="Account_Access" 
    SortExpression="Account_Access" />
<asp:BoundField DataField="sitename" HeaderText="owningsite" 
    SortExpression="sitename" />
<asp:BoundField DataField="sitename" HeaderText="Currentsitesetting" 
    SortExpression="sitename" />
<asp:CheckBoxField DataField="active" HeaderText="active" 
    SortExpression="active" />

2 个答案:

答案 0 :(得分:0)

如果我说得对,你需要根据OwningSite和CurrentSiteSetting显示字符串网站A 网站B 。您需要一个模板列(TemplateField)来记下将实际列值转换为模板值的逻辑。像下面的asp.net脚本(使用C#):

<asp:TemplateField HeaderText="Site Name (owning)">
    <ItemTemplate>
        <%# Eval("owningsite") == 1 ? "Site A" : "Site B" %>
    </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Site Name (setting)">
    <ItemTemplate>
        <%# Eval("Currentsitesetting") == 1 ? "Site A" : "Site B" %>
    </ItemTemplate>
</asp:TemplateField>

尝试使用上一个示例替换asp.net的以下部分。

<asp:BoundField DataField="sitename" HeaderText="owningsite" 
  SortExpression="sitename" />
<asp:BoundField DataField="sitename" HeaderText="Currentsitesetting" 
  SortExpression="sitename" />

希望有所帮助

答案 1 :(得分:0)

确定这可以使两个字段连接起来,但它显示出奇怪的结果,即为表中的每个网站名称获取同一个人的多条记录

SELECT        
Users.[First Name], Users.[User ID], Users.Login, Users.Surname, Users.Password, 
Users.[Account Question], Users.[Account Answer], Users.[Region Access], 
Users.[Account Access], Users.owningsite, Users.Currentsitesetting, Users.createdby, 
Users.createddate, Users.lastmodby, Users.lastmoddate, Users.active, 
Sites.Sitename AS owningsitename, Sites_1.Sitename AS Currentsitename

FROM             Users 

LEFT OUTER JOIN
                     Sites ON Users.owningsite = Sites.SiteID 

LEFT OUTER JOIN
                     Sites AS Sites_1 ON Users.Currentsitesetting = Sites.SiteID

WHERE        (Users.owningsite = 1)