Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 从ORACLE数据库抓取数据_Sql_Database_Oracle - Fatal编程技术网

Sql 从ORACLE数据库抓取数据

Sql 从ORACLE数据库抓取数据,sql,database,oracle,Sql,Database,Oracle,我有一个数据库,其中包含作者(id、姓名)、作者发布(aid、pid)、发布(id、标题) Author.id链接到AuthorPublication.aid,AuthorPublication.pid链接到Publication.od 我试图编写一个查询,返回与“amol”或“samuel”合著的作者的姓名,但不能同时返回两者 到目前为止我有 Select name From Author, AuthorPublication, Publication Where Publication.i

我有一个数据库,其中包含作者(id、姓名)、作者发布(aid、pid)、发布(id、标题)

Author.id链接到AuthorPublication.aid,AuthorPublication.pid链接到Publication.od

我试图编写一个查询,返回与“amol”或“samuel”合著的作者的姓名,但不能同时返回两者

到目前为止我有

Select name 
From Author, AuthorPublication, Publication
Where Publication.id = PID AND aid = Author.id 
在上面的代码中,我需要将PID过滤为PID与作者“samuel”或“amol”匹配的作者。但不是两者都有

作为oracle db的新手,我不太确定如何实现它,有什么帮助吗


提前谢谢

在where子句中尝试:
和(author.name='samuel'和author.name!='amol')或(author.name!='samuel'和author.name='amol')
(根据逻辑)。

?基本上,获取两个作者的id,获取他们的出版物的任何pid,这些pid不能相同……然后使用生成的出版物id。在一个查询中有几种方法可以做到这一点,下面是一种使用表别名在一个查询中多次使用表的方法:

select auth_sam.name as sams_name
      ,auth_amol.name as amols_name
      ,nvl(auth_sam.name, auth_amol.name) as single_author_name
      ,s_pubinfo.title as sam_publication_titles
      ,a_pubinfo.title as amol_publication_titles
      ,nvl(s_pubinfo.title, a_pubinfo.title) as single_pub_name
from author auth_sam
    ,authorPublication sam_pubs
    ,publication s_pubinfo -- 3 aliases for samuel data
    ,author auth_amol
    ,authorPublication amol_pubs
    ,publication a_pubinfo  -- 3 aliases for amol data
where auth_sam.name = 'samuel'
and auth_sam.id = sam_pubs.aid  -- pubs by samuel
and sam_pubs.pid = s_pubinfo.id  -- samuel titles
and auth_amol.name = 'amol'
and auth_amol.id = amol_pubs.aid  -- pubs by amol
and amol_pubs.pid = a_pubinfo.id  -- amol titles
and sam_pubs.pid != amol_pubs.pid  -- not the same publication
因为
=,查询将有效地返回两组结果。“samuel”的记录将填充“sams_name”列,“amols_name”列将为空。“amol”的记录将填充其名称列,并且samuel名称列值将为空。由于这些空值,我使用
NVL()
包含了两列,以演示选择要显示的作者字段值和标题字段值的方法。(这不是一个非常“干净”的解决方案,但我认为它展示了关系逻辑和SQL强大功能方面的一些见解。)


顺便说一句,在这个例子中,我真的认为使用Oracle SQL语法的SQL更具可读性。ANSI SQL版本包含所有的
JOIN
ON
关键字,我觉得读起来更难。

(1)学习使用带有
ON
子句的显式
JOIN
语法;(2) 逗号在
WHERE
子句中没有任何含义。哦,好吧,我会尝试加入,很好的提示!SQL有两个主要标准。使用t1,t2中的
,其中t1.key=t2.key
是oracle使用的SQL内部联接的合法方言@GordonLinoff使用关键字
JOIN
ON1