EID NAME SALARY DNAME HIRE_DATE NUM_PHONES
--- ------------------ ---------- ---------------------- --------- ----------
111 Yo-yo Ma 80000 Software 01-MAY-81 1
222 Leonard Bernstein 80000 Information Technology 02-JUN-82 2
333 Leonard Bernstein 110000 Research 03-JUL-83 1
444 Yehudi Menuhin 60000 Software 02-JAN-91 3
555 Lata Mangeshkar 50000 Software 01-MAY-81 5
666 Bing Crosby 65000 Tech Support 02-JUN-82 2
777 Frank Sinatra 40000 Tech Support 04-APR-84
DNAME BUDGET EXPENSE REGION
---------------------- ---------- ---------- ----------
Software 800000 600000 Eastern
Tech Support 2000000 3000000 Eastern
Information Technology 3000000 3000000 Western
Research 100000 60000 Western
EID PHONENUM
--- ----------
111 2372918458
222 5051734849
222 5755851934
333 1513664917
444 2206315710
444 3321045029
444 5678540401
444 5752658931
555 2546768967
555 5756390131
555 7201014829
EID PHONENUM
--- ----------
555 7595120111
555 7597209076
666 1513664917
666 5053241942
777 5753241942
在575区号中找到至少有一部手机的员工的ID和名称,但在505区号中没有。
我已尝试过以下操作,无法绕过双重eid属性。请帮忙!
SQL> select eid, phonenum, name
2 from phone, emp
3 where emp.eid = phone.eid and
4 phonenum like '575%'
5 and not like '505%'
6 ;
select eid, phonenum, name
*
ERROR at line 1:
ORA-00918: column ambiguously defined
答案 0 :(得分:0)
以575或505开头的电话号码位于不同的行中,因此它不像将两个LIKE条件应用于同一行那么简单。解决方案是创建子查询并使用条件检查我们要查找的内容是否存在而不使用另一个。
SELECT DISTINCT e.eid, e.name
FROM emp e
WHERE EXISTS(SELECT * FROM phone p1 WHERE p1.eid = e.eid AND p1.phonenum LIKE '575%')
AND NOT EXISTS(SELECT * FROM phone p2 WHERE p2.eid = e.eid AND p2.phonenum LIKE '505%')
答案 1 :(得分:0)
SQL> select eid, phonenum, name
2 from phone, emp
3 where emp.eid = phone.eid and
即使查询指定emp.eid = phone.eid
,第一行对eid
的引用也不明确。 (我认为这是后面的星号表示的。)
SQL要求对多个表中出现的列名的每个引用都要明确命名,不能相等。
答案 2 :(得分:0)
一个简单的解决方案是使用LEFT JOIN和存在条件:
SELECT DISTINCT e.eid, e.name
FROM emp
LEFT JOIN phone AS Area575 ON (Area575.eid = emp.eid) AND (Area575.phonenum LIKE '575%')
LEFT JOIN phone AS Area505 ON (Area575.eid = emp.eid) AND (Area575.phonenum LIKE '505%')
WHERE
Area575.eid IS NOT NULL AND
Area505.eid IS NULL