我们如何通过查看学生选择的工作内容来确定学生与公司的匹配情况,同时考虑到可用空缺并避免重复?
例如,如果学生选择IT作为他们首选的工作范围并且公司的工作范围是IT,那么如果公司只有空缺,我们如何在PHP上下文中提出查询以显示在网站上4名学生。
我们的目标是将学生分配到双方具有相同工作范围的特定公司,并显示学生所分配的公司名称,而不会重复。
我们无法发布数据库的屏幕截图,因此我们会将其列出。
表名:student_details
领域: -
名称
jobcope
表名:job_details
领域: -
job_title
jobcope
no_of_vacancy
到目前为止,这是我们从头开始制作的代码。
<?php
$con=mysqli_connect("host","user","password","database");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result13 = mysqli_query($con,"SELECT `admin_no`,`name`,`address`, `jobscope`,`GPA`,`gender` FROM `student_details` WHERE jobscope='IT' ORDER BY GPA DESC;");
echo "<table border='1' >
<tr bgColor=white>
<th>Admission Number</th>
<th>Name</th>
<th>Address</th>
<th>Jobscope</th>
<th>GPA</th>
<th>Gender</th>
</tr>";
while($row13 = mysqli_fetch_array($result13))
{
echo "<tr>";
echo "<td bgColor=white>" . $row13['admin_no'] . "</td>";
echo "<td bgColor=white>" . $row13['name'] . "</td>";
echo "<td bgColor=white>" . $row13['address'] . "</td>";
echo "<td bgColor=white>" . $row13['jobscope'] . "</td>";
echo "<td bgColor=white>" . $row13['GPA'] . "</td>";
echo "<td bgColor=white>" . $row13['gender'] . "</td>";
echo "</tr>";
}
echo "</table>";
$result12 = mysqli_query($con,"SELECT `job_id`,`job_title`,`jobscope`,`no_of_vacancy`,`job_address`,`contact_details` FROM`job_details` WHERE jobscope ='IT' ORDER BY `skill_id` ASC;");
echo "<table border='1' >
<tr bgColor=white>
<th>Job ID</th>
<th>Company Name</th>
<th>Jobscope</th>
<th>Number of Vacancy</th>
<th>Address</th>
<th>Contact Details</th>
</tr>";
while($row12 = mysqli_fetch_array($result12))
{
echo "<tr>";
echo "<td bgColor=white>" . $row12['job_id'] . "</td>";
echo "<td bgColor=white>" . $row12['job_title'] . "</td>";
echo "<td bgColor=white>" . $row12['jobscope'] . "</td>";
echo "<td bgColor=white>" . $row12['no_of_vacancy'] . "</td>";
echo "<td bgColor=white>" . $row12['job_address'] . "</td>";
echo "<td bgColor=white>" . $row12['contact_details'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
我们做了另一个问题来匹配学生和公司,但有很多重复。
SELECT DISTINCT s.name AS Student, j.job_title AS Company, s.jobscope
FROM student_details AS s,job_details AS j
WHERE s.jobscope = j.jobscope
AND s.status = '1';
是否有任何其他查询或函数可以防止同一查询本身出现重复?
当我们运行最后一个查询时,它将如下所示。
Student | Company | Jobscope
A2 | Razar (Asia Pacific) Pte Ltd | HR
A6 | Razar (Asia Pacific) Pte Ltd | HR
A10 | Razar (Asia Pacific) Pte Ltd | HR
A14 | Razar (Asia Pacific) Pte Ltd | HR
A18 | Razar (Asia Pacific) Pte Ltd | HR
A22 | Razar (Asia Pacific) Pte Ltd | HR
A2 | OAK 3 Films Pte Ltd | IT
A7 | OAK 3 Films Pte Ltd | IT
A11 | OAK 3 Films Pte Ltd | IT
A15 | OAK 3 Films Pte Ltd | IT
A19 | OAK 3 Films Pte Ltd | IT
A2 | CHILDREN'S CANCER FOUNDATION | HR
A6 | CHILDREN'S CANCER FOUNDATION | HR
A10 | CHILDREN'S CANCER FOUNDATION | HR
A14 | CHILDREN'S CANCER FOUNDATION | HR
A18 | CHILDREN'S CANCER FOUNDATION | HR
A22 | CHILDREN'S CANCER FOUNDATION | HR
为什么学生会有重复? 因为在student_details表中,每个学生都选择了自己喜欢的工作范围。但是当我们运行最后一个查询时,jobcope出现的方式不同了。
表:student_details
管理员没有|名称| Jobscope
2333456J | A2 | HR
2345677G | A7 | IT
答案 0 :(得分:0)
您需要进行一些聚合以消除您所谓的重复项。
这样的查询将为每个公司和范围组合提供学生列表。
SELECT j.job_title AS Company, s.jobscope, j.no_of_vacancy
group_concat(s.name) as students
FROM student_details AS s
JOIN job_details AS j ON s.jobscope = j.jobscope
WHERE s.status = '1'
GROUP BY j.job_title, s.jobscope, j.no_of_vacancy
ORDER BY j.job_title, s.jobscope, j.no_of_vacancy DESC
请注意group_concat和group_by操作。
以同样的方式,这将显示每个学生的公司。这些公司将首先展示最多的职位空缺。
SELECT s.name AS student, s.jobscope,
group_concat(j.job_title ORDER BY j.no_of_vacancy DESC) AS Companies
FROM student_details AS s
JOIN job_details AS j ON s.jobscope = j.jobscope
WHERE s.status = '1'
GROUP BY s.name, s.jobscope
ORDER BY s.name, s.jobscope
这将是一种混乱,因为公司领域可能相当广泛。但它会对你有用。
专业提示:加入我们的21世纪。避免使用JOIN(FROM a,b WHERE a.x=b.x
)的逗号列表样式,而是使用显式JOIN(FROM a JOIN b ON a.x=b.x
)。它也更容易阅读和使用LEFT JOIN。