从不同的表中查找记录

时间:2012-11-19 18:41:19

标签: sql oracle oracle10g

要求

  

查找并显示已实施的所有飞行员(员工ID和姓名)   在' Commuter'飞机上的航班类型和'正常'类型。

enter image description here

enter image description here

enter image description here

1st image is Plane_new table
2nd image is FlightI_new table
3rd image is Employee_new table

解决方案

SELECT flightI_new.eid, employee_new.ename
FROM flightI_new
INNER JOIN employee_new ON flightI_new.eid = employee_new.eid
INNER JOIN plane_new ON flightI_new.pnum = plane_new.pnum
WHERE plane_new.ptype = 'Commuter' AND plane_new.ptype = 'Normal';

代码与 OR 一起使用,但出于某些原因,我得到"没有返回任何行"。

知道怎么解决吗?

1 个答案:

答案 0 :(得分:1)

您的AND不起作用,因为记录不能同时记录ptype。但你应该可以使用这样的东西:

SELECT flightI_new.eid, employee_new.ename
FROM flightI_new
INNER JOIN employee_new 
    ON flightI_new.eid = employee_new.eid
INNER JOIN plane_new 
    ON flightI_new.pnum = plane_new.pnum
WHERE plane_new.ptype IN ('Commuter', 'Normal')
GROUP BY flightI_new.eid, employee_new.ename
HAVING count(distinct plane_new.ptype) = 2;

请参阅SQL Fiddle with Demo

你可以使用这样的东西:

SELECT distinct flightI_new."eid", employee_new."name"
FROM flightI_new
INNER JOIN employee_new 
    ON flightI_new."eid" = employee_new."eid"
INNER JOIN plane_new 
    ON flightI_new."pnum" = plane_new."pnum"
WHERE plane_new."ptype" = 'commuter'
  AND flightI_new."eid" IN (select flightI_new."eid"
                            FROM flightI_new
                            INNER JOIN employee_new 
                                ON flightI_new."eid" = employee_new."eid"
                            INNER JOIN plane_new 
                                ON flightI_new."pnum" = plane_new."pnum"
                            WHERE plane_new."ptype" = 'normal')

请参阅SQL Fiddle with Demo