微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java-是否可以在一个查询中组合2个请求,而仅在第一个不带任何内容的情况下才执行第二个查询?

假设我们有一张桌子的DVD:

id  title                   type    
 1  Star Wars               Movie
 2  Yellow Submarine        Music
 3  The Lord of The Rings   Movie
 4  Black Butterfly         Music

我们希望与电影“黑蝴蝶”一起获得DVD,但是如果列表中不存在该电影,则我们希望获得其他电影.
出生要求:

Select * from DVDs where type='Movie' and title='Black Butterfly'

如果请求什么都不返回,则执行第二个请求.

Select * from DVDs where type='Moview'

目前,我正在使用(在Java中)2个查询模板和对数据库(Oracle)的2个请求.我正在寻找使用1个模板和1个请求的机会.

解决方法:

你可以做:

with b as (
      Select *
      from DVDs
      where type = 'Movie' and title = 'Black Butterfly'
     )
select b.*
from b
union all
select d.*
from dvd
where type = 'Movie' and not exists (select 1 from b);

另外,您可以使用窗口函数

Select . . .
from (select d.*,
             count(*) filter (where title = 'Black Butterfly') over () as cnt_bb
      from DVDs d
      where type = 'Movie'
     ) d 
where cnt_bb = 0 or title = 'Black Butterfly';

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐