0

我有桌子

Employees
id
name 

Services
id
name

EmployeesServices
id_employee
id_service

我知道如果我做

select * from Employees inner join EmployeesServices on Employees.id = EmployessServices.Employee_id

编辑: AEmployeeServices有很多ServicesEmployees. 我需要知道我是否有一个Employee至少存在EmployeeServices一个Services

很抱歉造成混乱,但看起来很难

4

4 回答 4

1

我不完全确定我明白你在问什么。我也不完全确定您是否理解您的要求,但是当您尝试掌握 SQL 查询时,这并不罕见。

假设你有

  • Services.id 上的主键约束,以及
  • EmployeesServices(id_employee, id_service) 的主键约束

这应该返回所有“拥有”每项服务的员工。(在 PostgreSQL 中测试。)

select id_employee, count(id_employee)
from EmployeesServices
group by id_employee
having count(id_employee) = (select count(*) from Services)

EmployeesServices 中不能有重复的行。因此,员工 ID 号的计数(按员工 ID 号分组)必须与服务计数相匹配,以便员工“拥有”每一项服务。

于 2013-03-10T03:40:32.497 回答
1

id_service计算每个id_employeein的不同s EmployeesServices

SELECT
  id_employee,
  COUNT(DISTINCT id_service) AS service_count
FROM
  EmployeesServices
GROUP BY
  id_employee
;

现在,您需要过滤该service_count值。由于该表达式包含一个聚合函数,因此您需要使用一个HAVING子句,因为这是用于过滤行组的内容(与用于过滤的单个行相反WHERE)。

据我了解,条件应该是“每位员工的不同服务数量与服务表中的服务数量相同”。所以,我们开始:

SELECT
  id_employee,
  COUNT(DISTINCT id_service) AS service_count
FROM
  EmployeesServices
GROUP BY
  id_employee
HAVING
  COUNT(DISTINCT id_service) = (SELECT COUNT(*) FROM Services)
;

现在,这将只为您提供符合条件的员工的 ID。将查询用作派生表并将其加入Employee以访问员工的详细信息:

SELECT
  e.*,
  es.service_count
FROM
  Employee e
INNER JOIN (
  SELECT
    id_employee,
    COUNT(DISTINCT id_service) AS service_count
  FROM
    EmployeesServices
  GROUP BY
    id_employee
  HAVING
    COUNT(DISTINCT id_service) = (SELECT COUNT(*) FROM Services)
) es
ON
  e.id = es.id_employee
;

请注意,如果EmployeeServices不接受每个员工的重复服务,您可以替换COUNT(DISTINCT id_service)COUNT(*).

于 2013-03-10T03:42:36.643 回答
0

此查询将返回参与所有服务的所有员工的 ID:

SELECT es.id_employee 
FROM
(SELECT e.id as emp_id, s.id as serv_id FROM Employee e CROSS JOIN Service s) as full
LEFT JOIN EmployeeServices es ON es.id_employee=full.emp_id AND es.id_service=full.serv_id
GROUP BY full.emp_id
HAVING COUNT(*)=COUNT(es.id_employee);

SQL Fiddle - 您可能想要试验数据

于 2013-03-10T03:10:57.303 回答
0
    create table Employees (id number unique, name text);
    create table Services (Sid number unique, Sname text);
    create table EmployeesServices (EEid number, ESid number);
    insert into Employees values (1, 'bob');
    insert into Employees values (2, 'joan');
    insert into Employees values (3, 'philippe');
    insert into Services values (100, 'fell tree');
    insert into Services values (101, 'row boat');
    insert into Services values (102, 'read book');
    insert into Services values (103, 'sell book');
    insert into Services values (104, 'sell ticket');
    insert into EmployeesServices values (1, 100);
    insert into EmployeesServices values (2, 101);
    insert into EmployeesServices values (2, 101);
    insert into EmployeesServices values (3, 101);
    insert into EmployeesServices values (3, 103);
    insert into EmployeesServices values (3, 103);
    insert into EmployeesServices values (3, 104);
    insert into EmployeesServices values (1, 100);
    insert into EmployeesServices values (1, 101);
    insert into EmployeesServices values (1, 102);
    insert into EmployeesServices values (1, 102);
    insert into EmployeesServices values (1, 102);
    insert into EmployeesServices values (1, 102);
    select count(*) as Number_of_Unique_Services_Available from Services;
    select id, name, count(distinct Sid) as Unique_Services_Performed
    from Employees, Services, EmployeesServices
    where id = EEid and Sid = ESid
    group by id
    order by Unique_Services_Performed desc, name;

我创建了一些表进行测试。我假设员工可以多次执行相同的服务。第一个 select 语句告诉可用的唯一服务的数量。第二个创建输出,显示每个员工执行的唯一服务的数量。下面的示例输出显示了 5 个独特的服务,并且没有人执行所有这些服务。

    Number_of_Unique_Services_Available
    -----------------------------------
    5

    id          name        Unique_Services_Performed
    ----------  ----------  -------------------------
    1           bob         3
    3           philippe    3
    2           joan        1
于 2013-03-10T06:25:43.970 回答