部门明智的员工详细信息

时间:2016-03-29 18:43:08

标签: sql oracle10g

我有两个表员工和部门

1

create table department
(
    deptno int primary key,
    deptname varchar(20) not null,
    location varchar(50) not null
)

2

create table employee
(
    empno int primary key,
    empname varchar(20) not null,
    salary number(12,2) not null,
    comission number(10,2) not null,
    designation varchar(20) not null,
    deptno int references department(deptno),
    constraints chk_sal check(salary>0)
)

如果我想打印部门明智的员工详细信息,如

department name : <dept_name>
    <all employee details working in <dept_name>>
department name : <dept_name2>
    <all employee details working in <dept_name2>>

2 个答案:

答案 0 :(得分:0)

鉴于您的表结构,我们假设您有这样的数据:

部门:

deptno          deptname        location
----------------------------------------------------------------
  101           Juniors         The screwed cubicle
  102           Seniors         The little less screwed cubicle
  103           Super Seniors   The screwers

员工:

empno           empname                     salary      comission   designation       deptno
------------------------------------------------------------------------------------------------
  1         John Piledriven                 35000.00    2000.00     junior developer    101
  2         Ralph Chokeslam                 65000.00    3500.00     senior developer    102
  3         Ken Hurt                        40000.00    2000.00     junior developer    101
  4         Sue Yoo                         85000.00    3500.00     senior developer    102
  5         The One Who Shall Not Be Named  110000.00   5000.00     project manager     103

您可以使用以下查询:

SELECT          dep.deptno,
                dep.deptname,
                dep.location,
                emp.empname,
                emp.salary,
                emp.comission,
                designation

FROM            department dep
LEFT JOIN       employee emp ON dep.deptno = emp.deptno
ORDER BY        dep.deptno

你可以在这里查看 - &gt; http://sqlfiddle.com/#!9/c90e1/1

希望这有帮助!!!

答案 1 :(得分:-2)

在mysql中查询:

select e1.emp_id,e1.emp_name,d1.dept_id,d1.dept_name 
from employee e1,department d1,paydetail p1 
where e1.emp_id=p1.emp_id 
and d1.dept_id=p1.dept_id 
order by d1.dept_id;