所以我有一个查询,旨在查找并显示JOB_ID为SA_MAN和SA_REP的所有员工的部门,城市和国家名称,如下所示:
if(isset($_POST['button'])) {
$username = $_POST['name'];
$con = mysqli_connect("localhost","root","","test") or die ("Couldnt connect");
$check = "INSERT INTO name WHERE name='$username'";
$sql_check = mysqli_query($con,$check);
if($sql_check) {
echo "Successfully inserted";
} else {
echo 'Couldnt Insert';
}
}
但是,所有员工都驻留在一个部门中,因此对于SA_MAN或SA_REP的JOB_ID列中的每个员工,输出再次相同。如图所示:
SELECT DEPARTMENT_NAME, CITY, COUNTRY_NAME
FROM OEHR_EMPLOYEES
INNER JOIN OEHR_DEPARTMENTS ON OEHR_EMPLOYEES.DEPARTMENT_ID=OEHR_DEPARTMENTS.DEPARTMENT_ID
INNER JOIN OEHR_LOCATIONS ON OEHR_DEPARTMENTS.LOCATION_ID=OEHR_LOCATIONS.LOCATION_ID
INNER JOIN OEHR_COUNTRIES ON OEHR_LOCATIONS.COUNTRY_ID=OEHR_COUNTRIES.COUNTRY_ID
WHERE JOB_ID LIKE '%SA_MAN%' OR JOB_ID LIKE '%SA_REP%'
ORDER BY DEPARTMENT_NAME DESC
/
这继续34行。简单地说,我如何在查询中输出以反复停止不必要地显示相同数据并简单地显示一行。我希望如果查询发生更改并且仍然执行相同的操作,以便每个包含指定员工的部门只显示一行,就能够工作。
答案 0 :(得分:0)
FWIW,我发现这很容易阅读:
SELECT d.department_name
, l.city
, c.country_name
FROM oehr_employees e
JOIN oehr_departments d
ON d.department_id = e.department_id
JOIN oehr_locations l
ON l.location_id = d.location_id
JOIN oehr_countries c
ON c.country_id = l.country_id
WHERE job_id LIKE '%sa_man%'
OR job_id LIKE '%sa_rep%'
ORDER
BY department_name DESC
考虑修改你的job_id定义。这是你设计的一个弱点。