有人可以给出一个简单的解释或连接的例子吗?

时间:2017-06-29 07:27:09

标签: mysql sql

以下是问题的理论示例。我正在为一个企业制作数据库模式。

我有一个人员,员工,客户,生产,销售,会计和管理人力资源的表格。

  Person: 
  contact_id  (primary key)
  first_name
  last_name
  phone
  email

  Employee: 
  employee_id (primary key)
  contact_id (foreign key)  References Person(contact_id)
  date_start

  Production: 
  production_id (primary key)
  employee_id (foreign key) References Employee(employee_id)
  //other information

  Sales: 
  sales_id (primary key)
  employee_id (foreign key)  References Employee(employee_id)
  //other information

  Accounting: 
  accounting_id (primary key) 
  employee_id (foreign key) References Employee(employee_id)
  //other information


  Administration:
  administrator_id (primary key)
  employee_id (foreign key) References Employee(employee_id)
  //other information

  HumanResources:
  humanResources_id (primary key)
  employee_id (foreign key) References Employee(employee_id)
  //other information 

  Client
  client_id (primary key)
  contact_id (foreign key) References Person(contact_id)
  employee_id (foreign key) References Employee(employee_id)
  //other information

客户端是该表的错误名称。它主要跟踪客户与之联系的员工,我们可以根据员工当时所在的部门来假设对话的内容。

假设某个员工可能在多个部门工作,请返回在两个或三个公司部门中至少有一个部门工作的员工的名字和姓氏。

是这样的事情:

   SELECT b.first_name, b.last_name FROM Employee a 
   LEFT JOIN Person b USING(contact_id) 
   INNER JOIN Production c USING(employee_id) 
   WHERE a.employee_id IN (SELECT contact_id FROM Client)
   UNION    
   SELECT b.first_name, b.last_name FROM Employee a 
   LEFT JOIN Person b USING(contact_id) 
   INNER JOIN Accounting c USING(employee_id) 
   WHERE a.employee_id IN (SELECT contact_id FROM Client)
   UNION
   SELECT b.first_name, b.last_name FROM Employee a 
   LEFT JOIN Person b USING(contact_id) 
   INNER JOIN Sales c USING(employee_id) 
   WHERE a.employee_id IN (SELECT contact_id FROM Client);

2 个答案:

答案 0 :(得分:1)

这是一个奇怪的数据库设计。部门应该是数据,而不是实体。尽管您不希望为新员工创建新表,但只向employee表添加一行,但是在添加新部门而不是创建新部门时,您需要将记录添加到departments表中。表

所以考虑安装一个更好的数据库。 E.g:

  • employee(employee_id,fist_name,last_name,phone,...)
  • 部门(department_id,department_name,...)
  • employee_department(employee_id,department_id,salary,...)

答案 1 :(得分:1)

关于你原来的问题:这不仅仅是关于加入的问题。对于您的数据模型,您希望找到在生产,会计,销售三个部门中工作的人员。这是一个条件,因此属于WHERE子句。

select p.first_name, p.last_name
from person p
join employee e using (contact_id)
where e.employee_id in (select employee_id from production)
  and e.employee_id in (select employee_id from accounting)
  and e.employee_id in (select employee_id from sales);

甚至:

select first_name, last_name
from person
where contact_id in
(
  select contact_id
  from employee
  where employee_id in (select employee_id from production)
    and employee_id in (select employee_id from accounting)
    and employee_id in (select employee_id from sales)
);

如果您希望员工至少在两个部门工作,请计算:

select first_name, last_name
from person
where contact_id in
(
  select contact_id
  from employee e
  where 
    (select count(*) from production p where p.employee_id = e.employee_id) +
    (select count(*) from accounting a where a.employee_id = e.employee_id) +
    (select count(*) from sales s where s.employee_id = e.employee_id) >= 2
);

或使用EXISTS

select first_name, last_name
from person
where contact_id in
(
  select contact_id
  from employee e
  where exists (select * from production p where p.employee_id = e.employee_id) +
        exists (select * from accounting a where a.employee_id = e.employee_id) +
        exists (select * from sales s where s.employee_id = e.employee_id) >= 2
);

后者有效,因为在MySQL中,true = 1,false = 0。