3

GLC我需要列出有员工从事某项任务但没有员工从事某项任务的客户姓名及其项目名称CAC。**

我尝试了以下方法,但得到了一个空集:

select 
   c.custname, p.pjtitle
from 
   (select taskno pjno
    from task 
    where taskno not in
                 (select taskno
                  from task
                  where empid = 'CAC')
    and empid = 'GLC') t
join project p on p.pjno = t.pjno
join customer c on p.custno = c.custno;

我应该如何正确使用join和使用subquery

4

5 回答 5

4
SELECT c.custname, p.pjtitle
  FROM customer c
  JOIN project p ON p.custno = c.custno
  JOIN task t ON p.pjno=t.pjno
 WHERE t.empid='GLC'
   AND NOT EXISTS (SELECT NULL FROM task t1 WHERE t.pjno=t1.pjno AND t1.empid='CAC')
;
于 2013-06-15T06:26:26.270 回答
2

如果您想要客户名称和项目名称,那么大概您不想要重复项。这是 set-within-sets 子查询的示例。我认为最好的解决方案是使用聚合,因为它在描述条件时提供了最大的灵活性。

SELECT c.custname, p.pjtitle 
FROM customer c join
     project P
     ON P.CUSTNO = P.CUSTNO join
     task t ON t.pjno = p.pjno
group by c.custname, p.pjtitle 
having sum(case when empid = 'GLC' > 0 then 1 else 0 end) > 0 and
       sum(case when empid = 'CAC' > 0 then 1 else 0 end) = 0

have 子句的两个部分是计算每个员工在特定项目上工作的次数。第一个说“确保 GLC 至少处理一次任务”,第二个说“确保 CAC 没有处理任何任务”。

于 2013-06-15T07:06:34.347 回答
0

检查这个答案这应该工作

select c.custname, p.pjtitle 
  from project p, customer c where c.custno = p.custno 
and exists
(select t.pjno from task t where t.empid = 'GLC' and p.pjno = t.pjno 
and   t.pjno not IN (select pjno from task where empid = 'CAC'))
于 2013-06-15T07:02:52.623 回答
0

如果您只需要 GLC 员工的客户名称和项目名称,请使用以下查询

SELECT  c.custname ,
        p.pjtitle
FROM    Customer AS c
        INNER JOIN project AS p ON c.CUSTNO = p.CUSTNO
        INNER JOIN task AS t ON p.PJNO = t.PJNO
WHERE   t.EMPID = 'GLC'

如果您想同时使用员工 GLC 和 CAC,那么

SELECT  c.custname ,
        p.pjtitle
FROM    Customer AS c
        INNER JOIN project AS p ON c.CUSTNO = p.CUSTNO
        INNER JOIN task AS t ON p.PJNO = t.PJNO
WHERE   t.EMPID = 'GLC'
        AND t.EMPID = 'CAC'
于 2013-06-15T06:25:29.683 回答
0

尝试这个:

SELECT c.custname, p.pjtitle 
FROM customer AS c 
JOIN project AS P ON P.CUSTNO = P.CUSTNO
JOIN tack AS t ON t.pjno = p.pjno
WHERE t.empid = 'GLC'
AND NOT EXISTS (SELECT NULL FROM TASK WHERE empid = 'GLC' AND empid = 'CAC');
于 2013-06-15T06:31:54.530 回答