SQL Query不使用子查询

时间:2012-08-30 12:41:15

标签: sql oracle

我正在使用2张桌子

让我们说(这些表还有更多,但这只是一个非常简单的视图)

Table1 is
sID   ResearchPaperID 
1     A1
2     A1
3     A1
4     A1


Table2 is
sID    Name
1     Person1
2     Person2
3     Person3
4     Person4

我需要做的是找到与(共同撰写)Person1的论文相关联但未在输出中显示Person1的人。

目前我有类似

的东西
SELECT Table2.sID, Table1.sID
FROM Table2, Table1
WHERE Table2.sID = Table1.sID
AND Table2.Name = 'Person1'

这将为我提供Person1所做的一切。

这是一个家庭作业问题,现在已经停留了一段时间。

也不能使用子查询。 对不起,我应该提一下不使用内部连接,外部连接,左/右连接,自然连接。

它不应该比我上面使用基本命令更难。

[编辑] 输出将是这样的

ResearchPaperID    Name
A1                 Person2
A1                 Person3
A1                 Person4

我想找个人作为person1的论文的共同作者而不是在输出中显示person1

3 个答案:

答案 0 :(得分:1)

SELECT * 
FROM Table1 t1
INNER JOIN Table1 t2 ON t1.ResearchPaperID = t2.ResearchPaperID
INNER JOIN Table2 t3 ON t1.SID = t2.SID
WHERE t3.Name = 'Person1'

答案 1 :(得分:1)

您需要弄清楚如何识别论文的作者,然后假设其他人都是共同作者。一旦你能做到这一点,你的SQL查询将需要排除该主要编写者,但包括所有其他人。

答案 2 :(得分:1)

解决方案,没有子查询和JOIN:

select distinct t3.sID, t3.ResearchPaperID
from Table1 t1, Table2 t2, Table1 t3
where t1.sID = t2.sID and t2.Name = 'Person1'
  and t1.ResearchPaperID = t3.ResearchPaperID
  and t3.sID <> t2.sID       -- this restriction is the one that removes the 'Person1'
相关问题