admin

在Oracle中使用SQL从3个表中选择数据

sql

我希望那里有人可以提供帮助。我对所有这一切都 非常 陌生,并且被困住了。我有3个要从中提取数据的表:

客户(客户 ,头名,最后名,pointsclubno,调查号,订单日期,订单总价)

员工( Empid ,名字,姓氏,薪水,销售,salesdate,cusdid,locationid)

位置( locationid ,地址,城市,州)

我试图选择由员工服务的客户及其发生的地点。我首先尝试了此查询:

SELECT EMPLOYEE.EMPID, FIRST, LAST, LOCATION.LOCATIONID
FROM   EMPLOYEE, CUSTOMER, LOCATION  
WHERE  EMPID = '111111'
   AND EMPLOYEE.LOCATIONID = LOCATION.LOCATIONID;

未选择任何行。

然后这个查询:

SELECT EMPLOYEE.EMPID, FIRST, LAST, LOCATION.LOCATIONID
FROM   EMPLOYEE, CUSTOMER,LOCATION
WHERE  EMPID = '111111'
   AND EMPLOYEE.LOCATIONID = LOCATION.LOCATIONID;

 EMPID FIRST     LAST     LOCATIONID

111111 BOB       JONES      1267
111111 JIM       DAVIS      1267
111111 SCOTT     CONKLIN        1267
111111 DAN       ORTIZ      1267
111111 BRIAN     JOHNSON        1267
111111 TOM       MANN       1267
111111 LIZ       HENSLEY        1267
111111 BARB      BUTLER     1267
111111 MARIE     ANTON      1267
111111 JOE       SMITH      1267

已选择10行。

根据表的设置方式,此操作只能拉1行。如您所见,第一个查询拉0,第二个拉10。有人能看到我在做什么吗?


阅读 281

收藏
2021-07-01

共1个答案

admin

试试这个:[您没有加入客户和员工表]

SELECT 
EMPLOYEE.EMPID, 
FIRST, LAST, 
LOCATION.LOCATIONID 
FROM CUSTOMER, EMPLOYEE, LOCATION
WHERE 
CUSTOMER.CUSTID = EMPLOYEE.CUSTID AND
EMPLOYEE.LOCATIONID = LOCATION.LOCATIONID AND
AND EMPLOYEE.EMPID = '111111';
2021-07-01