sql查询一个包含子实体但返回父实体的域过滤

htzpubme  于 2021-08-20  发布在  Java
关注(0)|答案(2)|浏览(341)

嘿,我需要一些关于sql查询的帮助,我就是搞不懂。我得到了这两张表,它们具有一对一/多对一关系:

create table Author (
"UUID" RAW(16) DEFAULT NULL NOT NULL ENABLE,
"name" VARCHAR2(255 CHAR) NOT NULL ENABLE,
"processed" NUMBER(1,0) DEFAULT 0 NOT NULL ENABLE,
CONSTRAINT "Author_P" PRIMARY KEY ("UUID"),
)

create table Book (
"UUID" RAW(16) DEFAULT NULL NOT NULL ENABLE, 
"name" VARCHAR2(255 CHAR) NOT NULL ENABLE,
"release" TIMESTAMP(6),
"AUTHOR_ID" RAW(16) NOT NULL,
 CONSTRAINT Book_P PRIMARY KEY (UUID),
        CONSTRAINT Book_F FOREIGN KEY (KAFKA_BEZIEHUNG_ID) REFERENCES "Author" ("UUID")
)

现在,我获得了这个select查询,以获取当前日期之前编写的所有书籍:
(我通过查询注解在java jpa中执行此操作)

@Query(Select b from Book b where b.release < current_timestamp)
List<Book> findAllBooksBeforeToday();

在这种情况下,我将归还所有书籍,但我所做的是:
仅返回作者,不返回重复项
另外,我只想返回“processed”值为false的作者。
我可以通过这样做或至少我认为这样做来实现第一个目标,但这并不奏效:

@Query(Select b.author_id from Book b where b.release < current_timestamp)
HashSet<Author> findAllBooksBeforeToday();

第二个条件非常简单,如下所示:

@Query(Select a from author a where a.processed = false)

所以我现在的问题是,我如何,甚至可能我可以将这两个查询合并到一个查询中,从book表中选择,但只返回被处理的author=false?

sxpgvts3

sxpgvts31#

我假设book.author\u id与author.uuid匹配

select distinct b.author_id 
from Book b inner join Author a on a.UUID = b.AUTHOR_ID
where a.processed = 0 and b.release < current_timestamp
ut6juiuv

ut6juiuv2#

我不懂java,但是-可以实现您想要的功能(至少,我是如何理解的)的查询是

-- distinct authors (i.e. no duplicates)
select distinct a.name
from author a;

-- "false" processed authors
select distinct a.name
from author a
where a.processed = 0;

-- join books and "false" processed authors
select b.name as book_name,
       a.name as author_name
from book b join author a on a.uuid = b.uuid
where a.processed = 0;

相关问题