-1

我正在尝试选择教师的姓名、邮政编码和电话号码,这些教师的学生与教师居住在相同的邮政编码中

首先我选择 zip 学生和教师有相同的 ZIP

SELECT ZIP 
FROM STUDENT
INTERSECT
SELECT ZIP
FROM INSTRUCTOR;

它给了我我想要的

ZIP 
-----
10025

那么我现在如何从我已经选择的 ZIP 中选择名字、姓氏和电话呢?我怎么能把它们结合起来?

SELECT FIRST_NAME,LAST_NAME,PHONE
FROM INSTRUCTOR w, STUDENT s
WHERE w.STUDENT_ID = w.STUDENT_ID
GROUP BY FIRST_NAME,LAST_NAME,PHONE
HAVING COUNT(*) =
(SELECT ZIP
FROM
(SELECT ZIP 
FROM STUDENT
INTERSECT
SELECT ZIP
FROM INSTRUCTOR));
4

2 回答 2

1

您要选择教师详细信息和学生详细信息吗?如果是这样试试

SELECT FIRST_NAME,LAST_NAME,PHONE 
FROM
    (
    SELECT FIRST_NAME,LAST_NAME,PHONE,ZIP
    FROM INSTRUCTOR
    UNION ALL
    SELECT  FIRST_NAME,LAST_NAME,PHONE ,ZIP
    FROM STUDENT
    ) 
Derived
 WHERE ZIP IN (SELECT ZIP FROM STUDENT 
               INTERSECT
               SELECT ZIP
               FROM INSTRUCTOR)

更新:如果你只需要导师,一个简单的加入就足够了

SELECT FIRST_NAME,LAST_NAME,PHONE,ZIP
FROM INSTRUCTOR INNER JOIN STUDENT ON INSTRUCTOR.ZIP = STUDENT.ZIP
于 2013-10-29T22:52:48.057 回答
0

我建议加入 zip 字段上的两个表。

于 2013-10-29T22:49:46.057 回答