SQL C# 过滤相似记录

SQL C# Filter Similar Records

问题与我之前问过的一个问题有关,但我需要一点帮助...

我有兴趣 table,它存储了 SeekersEmployers...

的兴趣

兴趣 table 就像:

int_id   FK_is_ID  FK_ie_ID  FK_ic_ID
-------------------------------------
1           2          null      3
2           2          null      5
3           null       1         3
4           null       3         1
5           null       2         5
6           4          null      1
6           3          1         2

FK_is_id = Seeker ID
FK_ie_id = Employer ID
FK_ic_id = Category ID

因此,当搜索者登录时,我会尝试检索 EMPLOYER 与已登录搜索者有相似兴趣的姓名...

我有一个代码可以正常工作,给出我想要的输出..

SqlConnection myConn2;
SqlCommand myCommand2;
SqlDataReader myReader2;
String SQL2, divjobs;

myConn2 = new SqlConnection(WebConfigurationManager.ConnectionStrings["ApplicationServices"].ToString());
divjobs = "<ul>";
myConn2.Open();
SQL2 = "SELECT ins.int_id, ins.FK_is_ID, ine.FK_ie_ID,emp.e_name,ine.int_id FROM  Interests ins INNER JOIN Interests ine ON ins.FK_ic_ID = ine.FK_ic_ID FULL OUTER JOIN employers emp ON emp.e_id=ine.FK_ie_id WHERE  ins.FK_is_ID = @sid  AND ine.FK_ie_ID IS NOT null order by emp.e_name ASC";

myCommand2 = new SqlCommand(SQL2, myConn2);
myCommand2.Parameters.AddWithValue("@sid", ssid);

myReader2 = myCommand2.ExecuteReader();

if (myReader2.HasRows)
{
    while (myReader2.Read())
    {
        divjobs = divjobs + "<li>" + "<a href='../employers/viewemployer.aspx?EID=" + myReader2["FK_ie_id"] + "'>" + "<div class='jtitle'>" + myReader2["e_name"] + "</div>" + "</li>";
    }
    divjobs = divjobs + "</ul>";
    joblist.InnerHtml = divjobs;
}
else
{
    joblist.InnerHtml = "<div style='font-weight:bold;padding:20px;font-size:1.2em;background-color:white;'>"+ "Looks like there is nothing to load!" + "</div>";
}

myConn2.Close();

唯一的问题是,如果雇主有 2 个兴趣,输出会给出相同的雇主名称两次!我希望它只来一次... !

有没有办法做到这一点,任何帮助将不胜感激...?

目前,您的结果处于 "interest" 级别,但您确实希望在雇主级别获得结果 - 以下将 return 具有匹配兴趣的雇主的不同列表 - 我也扔了每个匹配兴趣的聚合(计数):

SELECT
    ins.FK_ie_ID,
    emp.e_name,
    COUNT(ins.int_id) AS InterestCount
FROM
Interests ins 
INNER JOIN 
Interests ine 
    ON ins.FK_ic_ID = ine.FK_ic_ID 
INNER JOIN 
employers emp 
    ON emp.e_id = ine.FK_ie_id 
WHERE  
    ins.FK_is_ID = @sid  
GROUP BY 
    ins.FK_ie_ID,
    emp.e_name
order by emp.e_name ASC

希望我正确理解了您的需求。

此外,我发现您的列命名非常难用。因此,查询中很可能存在拼写错误。它们如此神秘是有原因的……还是它们超出了您的控制范围?