简体   繁体   English

MySQL-查询输出合并数据

[英]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: 因此,我有一个查询旨在查找和显示具有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
/

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. 但是,所有雇员都驻留在一个部门中,因此对于SABMAN或SA_REP的JOB_ID列中的每个雇员,输出都是相同的。 Like shown: 如图所示:

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

This continue for 34 rows. 这继续进行34行。 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: 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

Consider amending your job_id definitions. 考虑修改job_id定义。 This is a weakness in your design. 这是设计中的弱点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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