我正在使用2个表,雇主表和工作表.
雇主表的值如下:
E_ID e_name
1 john
2 rick
3 mike
Jobs表的值如下:
J_ID FK_eID J_Title
1 1 Job1
2 1 Job2
3 3 Job3
4 2 Job4
5 3 Job5
6 1 Job6
所以工作是由雇主创造的,
我要过滤的是哪个雇主发布了最多的职位,并在列表中以最大到最小顺序显示e_name …
我正在使用的代码(不了解SQL部分):
SqlConnection myConn2;
SqlCommand myCommand2;
SqlDataReader myReader2;
String SQL2,SQL, divjobs;
myConn2 = new SqlConnection(WebConfigurationManager.ConnectionStrings["ApplicationServices"].ToString());
divjobs = "<ul>";
myConn2.Open();
SQL2 = "";
myCommand2 = new SqlCommand(SQL2, myConn2);
myReader2 = myCommand2.ExecuteReader();
while (myReader2.Read())
{
divjobs = divjobs + "<li>" + "<a href='employers/viewemployer.aspx?EID=" + myReader2["e_id"] + "'>" + myReader2["e_name"] + "</a>" + "</li>";
}
divjobs = divjobs + "</ul>";
topemp.InnerHtml = divjobs;
myConn2.Close();
解决方法:
按雇主分组并按每个职位的工作顺序排序
select e.e_name, count(j.j_id) as jobs
from employer e
left join jobs j on j.fk_eid = e.e_id
group by e_id, e.e_name
order by count(j.j_id) desc