Oracle SQL查询仅涉及提取某些项目

时间:2015-02-28 08:38:03

标签: sql oracle oracle11g

所以我有这三个表:

WORKERS( WNO, WNAME, ZIP, HDATE )

CUSTOMERS( CNO, CNAME, STREET, ZIP, PHONE )

ORDERS( ONO, CNO, WNO, RECEIVED, SHIPPED )

我想找到那些向与工人生活在同一邮政编码中的客户进行销售的员工。到目前为止,我有这段代码:

SELECT e.wno
FROM ORDERS o, CUSTOMERS c, WORKERS e
WHERE o.cno = c.cno AND o.wno = e.wno AND c.zip = e.zip

但这并没有给出所需的答案。谁能帮我这个?如果你可以帮忙的话会很棒。

ONO      CNO         WNO RECEIVED  SHIPPED         CNO CNAME                   STREET                      ZIP     PHONE             WNO WNAME                     ZIP HDATE
1021     1111       1000 12-JAN-95 15-JAN-95       1111 Charles                123 Main St.                67226 316-636-5555       1000 Jones                   67226 12-DEC-95
1020     1111       1000 10-DEC-94 12-DEC-94       1111 Charles                123 Main St.                67226 316-636-5555       1000 Jones                   67226 12-DEC-95
1022     2222       1001 13-FEB-95 20-FEB-95       2222 Bertram                237 Ash Avenue              67226 316-689-5555       1001 Smith                   60606 01-JAN-92
1026     2222       1001 03-JAN-96 08-JAN-96       2222 Bertram                237 Ash Avenue              67226 316-689-5555       1001 Smith                   60606 01-JAN-92
1023     3333       1000 20-JUN-97                 3333 Barbara                111 Inwood St.              60606 316-111-1234       1000 Jones                   67226 12-DEC-95
1024     4444       1002 13-APR-95 17-APR-95       4444 Jorge                  131 State St.               47907 765-111-1884       1002 Brown                   50302 01-SEP-94
1025     4444       1000 25-APR-95 30-APR-95       4444 Jorge                  131 State St.               47907 765-111-1884       1000 Jones                   67226 12-DEC-95

4 个答案:

答案 0 :(得分:1)

试试这个:

SELECT e.wno
FROM ORDERS o join  CUSTOMERS c on o.cno = c.cno
join WORKERS e on o.wno = e.wno
Group by e.wno,c.zip
having count(*)=1

答案 1 :(得分:1)

我认为您的原始查询只会添加NOT EXISTS子句:

SELECT e.wno
  FROM workers e, orders o, customers c
 WHERE e.wno = o.wno
   AND o.cno = c.cno
   AND c.zip = e.zip
   AND NOT EXISTS ( SELECT 1 FROM orders o1, customers c1
                     WHERE o1.wno = e.wno
                       AND o1.cno = c1.cno
                       AND c1.zip != e.zip );

也就是说,让所有以自己的邮政编码交付给客户但未在邮政编码之外交付的员工。您可能也希望使用ANSI连接(我必须承认我对这种语法不太满意所以我并不是100%确定连接是正确的):

SELECT e.wno
  FROM workers e INNER JOIN orders o
    ON e.wno = o.wno
 INNER JOIN customers c
    ON o.cno = c.cno
 WHERE c.zip = e.zip
   AND NOT EXISTS ( SELECT 1 FROM orders o1 INNER JOIN customers c1
                        ON o1.cno = c1.cno
                     WHERE o1.wno = e.wno
                       AND c1.zip != e.zip );

答案 2 :(得分:0)

一种方法是找到所有按照不同的拉链销售产品的工人,然后做一个减号(或不是IN)。

SELECT e.wno
FROM WORKERS e
WHERE e.wno NOT IN (
    SELECT e.wno
    FROM ORDERS o, CUSTOMERS c, WORKERS e
    WHERE o.cno = c.cno AND o.wno = e.wno AND c.zip != e.zip)

答案 3 :(得分:0)

可能低于查询将帮助您:)

select * from WORKERS WR,CUSTOMERS CR,ORDERS OR 
where 
WR.ZIP=OR.ZIP
AND CR.CNO=OR.CNO
AND WR.WNO=OR.WNO
AND OR.RECEIVED is not null;

" OR.RECEIVED不为空"是确保客户收到他/她的物品的条件。