如何在Oracle SQL中使用JOIN运算符?

时间:2019-05-23 12:30:46

标签: sql database oracle

我具有以下学者关系模式:

ACADEMIC(acnum, deptnum*, famname, givename, initials, title)
PAPER(panum, title)
AUTHOR(panum*, acnum*)

我被要求退回名额,姓氏,家族名称以及他/她为每位学者撰写的论文总数。

我不知道如何在表之间建立连接。

SELECT academic.ACNUM, academic.GIVENAME, academic.FAMNAME
FROM paper,
     author,
     academic
WHERE author.PANUM = paper.PANUM
GROUP BY academic.ACNUM, academic.GIVENAME, academic.FAMNAME, count(author.PANUM = paper.PANUM);

2 个答案:

答案 0 :(得分:0)

这应该工作-

SELECT AC.ACNUM, AC.GIVENAME, AC.FAMNAME,COUNT(*)
FROM paper P
INNER JOIN author A ON P.panum = A.panum
INNER JOIN ACADEMIC AC ON A.acnum = AC.acnum
GROUP BY  AC.ACNUM, AC.GIVENAME, AC.FAMNAME

答案 1 :(得分:0)

要为初学者构建SQL(Oracle语法),请执行以下步骤:

制定一个空的SQL方案

SELECT
FROM  
WHERE
GROUP BY

填写表(带别名):

FROM paper    pap,
     author   aut,
     academic aca

在WHERE子句中添加联接

FROM  paper    pap,
      author   aut,
      academic aca
WHERE aut.panum = pap.panum
AND   aut.acnum = aca.deptnum

然后选择所需的列

SELECT aca.acnum
      ,aca.givename
      ,aca.famname
      ,count (pap.title) as number_titles
FROM   paper    pap
      ,author   aut
      ,academic aca
WHERE  aut.panum = pap.panum
AND    aut.acnum = aca.acnum

要计数时,请添加GROUP BY

SELECT aca.acnum
      ,aca.givename
      ,aca.famname
      ,count (pap.title) as number_titles
FROM   paper    pap
      ,author   aut
      ,academic aca
WHERE  aut.panum = pap.panum
AND    aut.acnum = aca.acnum
GROUP  BY aca.acnum
         ,aca.givename
         ,aca.famname

然后我们开始。上面是Oracle Syntas,下面是ANSI SQL,效果很好:

SELECT aca.acnum
      ,aca.givename
      ,aca.famname
      ,count (pap.title) as number_titles
FROM       paper    pap
INNER JOIN author   aut ON aut.panum = pap.panum
INNER JOIN academic aca ON aut.acnum = aca.acnum
GROUP  BY aca.acnum
         ,aca.givename
         ,aca.famname