注册 登录
编程论坛 SQL Server论坛

从多条记录提取数据的一个问题,我想了2天也没想出来!求助!

twlk 发布于 2004-10-04 23:32, 1250 次点击
两个表:
1---------------------------------------------
create table Department (
dpt_no       number(2)
  constraint pk_department PRIMARY KEY,
dpt_name     varchar2(25)
  constraint nn_dpt_name NOT NULL,
mgr_ssn       char(9),
mgr_start_date date
);
2----------------------------------------------
create table Employee (
  emp_ssn           char(9)
    constraint pk_employee PRIMARY KEY,
  emp_last_name       varchar2(25)
    constraint nn_emp_last_name NOT NULL,
  emp_first_name     varchar2(25)
    constraint nn_emp_first_name NOT NULL,
  emp_birth_date     date,
  emp_address       varchar2(50),
  emp_sex           char(1),
  emp_salary         number(7,2),
  emp_superssn       char(9),
  emp_dpt_no         number(2),
CONSTRAINT fk_emp_superssn FOREIGN KEY (emp_superssn)
  REFERENCES employee
     ON DELETE SET NULL,
CONSTRAINT fk_emp_dpt_no FOREIGN KEY (emp_dpt_no)
  REFERENCES department
     ON DELETE SET NULL
);
=================================================
值:
=========================================================
insert into Department
values ('3','Research',NULL,NULL);
insert into Department
values ('5','Design',NULL,NULL);
....................
insert into Department_locations
values ('3','Mout');
insert into Department_locations
values ('5','Meadow');
....................
insert into Project
values ('01','Strom','Mout','3');
insert into Project
values ('02','Wind','Mout','3');
insert into Project
values ('03','CBS','Mout','5');
--------------------------------------------------------------------
insert into employee
values ('420124', 'Green', 'Jim', '05/JUN/82', '23-1 Park St.', 'M','75000',NULL, '3');
insert into employee
values ('420123', 'Brodoloi', 'Elsa', '12/SEP/81', '24-2 King St.', 'F','45000', '420124', '3');
insert into employee
values ('420122', 'Queen', 'Sonia', '15/DEC/80', '12 Church St.', 'F','50000', '420124', '3');
insert into employee
values ('420121', 'King', 'Albert', '25/JUN/77', '45 Beach St.', 'M','52000', '420124', '3');
insert into employee
values ('420125', 'Davi', 'Jeffery', '23/DEC/79', '12-1 Queen St.', 'M','65000', NULL, '5');
insert into employee
values ('420126', 'Green', 'Austin', '04/AUG/78', '11 Princess St.', 'M','55000', '420125', '5');
insert into employee
values ('420127', 'Win', 'Angus', '22/AUG/83', '08 Robson St.', 'M','52000', '420125', '5');
...................

问题:
查找那些在2个或多个部门工作的员工的姓名!

在此求助,大家帮帮忙!
5 回复
#2
longhao2004-10-06 12:34

select emp_last_name,emp_first_name

from Employee

inner join Department

on Department.dpt_no=Employee.emp_dpt_no

我认为是采用连接方式来连接,大家觉得呢,(我是个初学者~)

#3
twlk2004-10-09 22:30

上面的表有些问题,我是想问一个员工如果可以在多个部门工作,我想提取出来在2个或多个部门工作的员工的名字,如果员工只在一个部门工作则不要

#4
database2004-11-04 20:25

select ssn,emp_first_name,emp_last_name,count(emp_dpt_no )

from Employee

gruop by ssn

having count(emp_dpt_no )>=2

仅供参考!还请多提宝贵意见!

#5
何凡心2004-11-05 18:00
以下是引用database在2004-11-04 20:25:38的发言:

select ssn,emp_first_name,emp_last_name,count(emp_dpt_no )

from Employee

gruop by ssn

having count(emp_dpt_no )>=2

仅供参考!还请多提宝贵意见!

#6
lzalibabalr2007-08-01 16:26

SELECT RTRIM(emp_last_name) + ' ' + RTRIM(emp_first_name) AS '员工姓名'
FROM Employee INNER JOIN Department
ON Empolyee.em_dpt_no = Department.dpt_no

GROUP BY RTRIM(emp_last_name) + ' ' + RTRIM(emp_first_name),
dpt_no
HAVING COUNT(RTRIM(emp_last_name) + ' ' + RTRIM(emp_first_name))>1

1