简体   繁体   English

从ORACLE DB获取数据

[英]Grabbing data from ORACLE DB

I have a database which has Author(id, name), AuthorPublication(aid, pid), Publication(id, title) 我有一个数据库,其中包含Author(id,name),AuthorPublication(aid,pid),Publication(id,title)

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

I am trying to write a query that returns the name of authors who has co-authored with "amol" or "samuel" BUT NOT BOTH. 我正在尝试编写一个查询,该查询返回与“ amol”或“ samuel”共同创作的作者的名字,但两者都不是。

So far I have 到目前为止,我有

Select name 
From Author, AuthorPublication, Publication
Where Publication.id = PID AND aid = Author.id 

In the above code I need to filter the PID to be authors whose pid matches author "samuel" or "amol". 在上面的代码中,我需要将PID过滤为pid与作者“ samuel”或“ amol”匹配的作者。 But not both 但不是两者

Being new to oracle db, Im not so sure how to implement this, any help? 作为oracle db的新手,我不太确定如何实现此功能,有什么帮助吗?

Thanks in advance! 提前致谢!

在您的where子句中尝试: and (author.name = 'samuel' and author.name != 'amol') OR (author.name != 'samuel' and author.name = 'amol') (根据https:/ /community.oracle.com/thread/2342467?tstart=0 )。

Logic? 逻辑? Basically, get the id of the two authors, get any pid of their publications, those pids can't be the same....then use the resulting publication ids. 基本上,获取两个作者的ID,获取其出版物的任何pid,这些pid不能相同....然后使用生成的出版物ID。 There are several ways to do this in 1 query, here's one way using table aliases to use tables more than once in one query: 1个查询中有几种方法可以执行此操作,这是一种使用表别名在一个查询中多次使用表的方法:

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

Because of the != , the query effectively returns 2 sets of results. 由于!= ,该查询有效地返回2组结果。 Records for 'samuel' will have the 'sams_name' column populated and the 'amols_name' column will be null. “ samuel”的记录将填充“ sams_name”列,而“ amols_name”列将为空。 Records for 'amol' will have his name column populated and the samuel name column value will be null. “ amol”的记录将填充其“名称”列,而“塞缪尔名称”列的值将为null。 Because of these nulls, I included two columns using NVL() to demonstrate a method to choose which author field value and title field value to display. 由于这些空值,我使用NVL()包括了两列,以演示选择要显示哪个作者字段值和标题字段值的方法。 (Not a very "clean" solution, but I think it demonstrates several insights in the power of relational logic and SQL.) (这不是一个非常“干净”的解决方案,但我认为它展示了有关关系逻辑和SQL功能的一些见解。)

btw - in this example, I really think the SQL is more readable with the Oracle SQL syntax. 顺便说一句-在这个例子中,我真的认为SQL使用Oracle SQL语法更具可读性。 The ANSI SQL version, with all the JOIN and ON keywords, feels more difficult to read to me. 带有所有JOINON关键字的ANSI SQL版本对我来说更难理解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM