简体   繁体   中英

MySQL - Query Output Merging Data

So I have a query designed to find and display the Department, City and Country Names for all Employees with JOB_IDs of SA_MAN and SA_REP which is as follows:

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
/

However all the Employees reside in one department so the output is just the same over again for every employee in the JOB_ID column with SA_MAN or SA_REP. Like shown:

DEPARTMENT_NAME                COUNTRY_NAME                             CITY
------------------------------ ---------------------------------------- ----------------------------
Sales                          United Kingdom                           Oxford
Sales                          United Kingdom                           Oxford
Sales                          United Kingdom                           Oxford

This continue for 34 rows. Simply put how can I make the output in the query to stop this needless display of the same data over and over again and simply just display one row. I want this to be able to also work if the query is change and still do the same thing so that only one row per department containing the specified employees shows up.

FWIW, I find this MUCH easier to read:

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

Consider amending your job_id definitions. This is a weakness in your design.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM