这是我遇到的问题。
我正在尝试为程序构建查询。这是查询:
这是我的表格
employee
表:fname,lname,ssn,birthdate,address,dno(部门编号)project
表:projectname,projectnumber,plocation,dnum(部门编号)works_on
表:ssn,projectnumber,hours_worked department
表:dname,dnumber,managerssn,manager_start_date 这是我在工作几个小时后想出来的:
select distinct
employee.fname,
employee.lname,
employee.ssn,
employee.dno,
project.pnumber,
project.dnum
from
employee, works_on, project
where
employee.dno = project.dnum
and project.pnumber = works_on.pno;
答案 0 :(得分:0)
select e.fname, e.lname, e.dno, p.projectname
from employees e
inner join projects p on p.projectnumber = e.project --OR HOWEVER THE 2 ARE LINKED
group by e.fname, e.lname, e.dno, p.projectname
having count(p.Projectnumber) = 1
order by e.lname asc
答案 1 :(得分:0)
SELECT
e.ssn as employee_ssn
,e.dno as employee_department_number
,wo.projectnumber
,p.projectname as project_name
,p.dnum as project_department_number
FROM employees e
JOIN works_on wo on wo.ssn = e.ssn
JOIN projects p on p.projectnumber = wo.projectnumber
WHERE e.dno = p.dnum
答案 2 :(得分:0)
SELECT
e.fname
, e.lname
FROM works_on w
INNER JOIN employee e
ON w.ssn = e.ssn
INNER JOIN project
ON w.projectnumber = p.projectnumber
AND e.dno = p.dnum
GROUP BY
e.fname
, e.lname
HAVING COUNT(w.projectnumber) = 1
“只有一个由他们部门控制的项目”
答案 3 :(得分:0)
因此我们需要在员工,项目和工作中进行联接。我们可以忽略部门,因为它并不重要。这是我认为可行的查询:
select fname, lname, dno, projectname, count(*) as project_count
from employee, project, works_on
where employee.ssn = works_on.ssn and
works_on.projectnumber = project.projectnumber
project.dnum = employee.dno
having project_count = 1
根据您使用的SQL引擎,HAVING子句的语法可能略有不同。
答案 4 :(得分:0)
不确定您是否需要GROUP BY,取决于您的意思"恰好是一个项目"。这应该非常接近:
select e.fname, e.lname, p.projectname, p.location, d.dname FROM
employee e
INNER JOIN works_on w
ON e.ssn = w.ssn
INNER JOIN project p
ON w.projectnumber = p.projectnumber
INNER JOIN department d
ON p.dnum = d.dnumber
WHERE e.dno = p.dnum
ORDER BY e.lname, e.fname