0

要求

查找并显示在“通勤”类型和“普通”类型的飞机上执行航班的所有飞行员(员工 ID 和姓名)。

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

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,但由于某种原因,我得到“没有返回行”。

知道如何解决吗?

4

2 回答 2

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

你可以使用这样的东西:

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

于 2012-11-19T18:46:29.877 回答
0

错误就在这里

      WHERE plane_new.ptype = 'Commuter' AND plane_new.ptype = 'Normal';

plane_new.ptype 在你的数据库中不能有两个东西,computer 和 normal,它必须是其中之一,这就是为什么你没有返回任何行。所以也许你的意思是别的。在您的 WHERE 语句中,您只能使用OR. 他们返回的行是那些拥有computernormal

于 2012-11-19T19:17:18.263 回答