【问题标题】:Is there a way to nesting one select statement to another one?有没有办法将一个选择语句嵌套到另一个?
【发布时间】:2019-12-04 23:26:12
【问题描述】:
select regexp_replace('home care','home care','care') as column1,
       regexp_replace('home care','home care','home') as column2
  from WORDLIST w

输出:

column1 column2

 care    home

然后

select regexp_replace('care home','care home','home') as column3,
       regexp_replace('care home','care home','care') as column4
  from WORDLIST w

输出:

column3 column4

  home     care

所以现在我想通过将一个 select 语句嵌套到另一个没有“join”字且也没有 where 子句的 select 语句来将这两个正则表达式连接在一起,输出应该如下:

column1 column2 column3 column4

  care   home  home     care

如果有任何建议,我将不胜感激!

【问题讨论】:

  • 为什么从WORDLIST表中选择而不使用呢?无论如何,select regexp_replace('home care','home care','care') as column1, regexp_replace('home care','home care','home') as column2, regexp_replace('care home','care home','home') as column3, regexp_replace('care home','care home','care') as column4 from WORDLIST w 会得到你想要的……我不明白为什么会想要这些。也许你只是给我们举了一个可怕的例子?
  • @JNevill 是的,这就是我在这里寻求帮助的原因,我正在学习正则表达式。首先,我需要为四列分配别名,并将其中一列嵌套到另一列(连接操作),并且没有 where 子句,没有“连接”字样。所以我现在真的很困惑。有没有办法做到这一点?
  • 我的意思是......你的问题没有多大意义。为什么要从WORDLIST 表中选择?您可以从dual 中选择并获得相同的结果(或任何具有单个记录的表),因此这是多余的。您有两个 SELECT 语句从同一个表中选择,因此不需要连接,只需将一个 SELECT 子句中的列添加到另一个 SELECT 子句中,就像我在之前的评论中所做的那样。无需嵌套、连接或任何其他魔法。

标签: sql regex oracle join


【解决方案1】:

下面的代码应该可以工作,它确实使用了 'join on 1 = 1' 的强制连接

select
w1.column1
,w1.column2
,w2.column3
,w2.column4
from(
select regexp_replace('home care','home care','care') as column1, regexp_replace('home care','home care','home') as column2 from WORDLIST w
) as w1
left join (
select regexp_replace('care home','care home','home') as column3, regexp_replace('care home','care home','care') as column4 from WORDLIST w
) as w2 on 1 = 1

【讨论】:

  • 谢谢先生,这几乎就是我想要的,但是有没有办法不用“加入”这个词来做到这一点?
  • 我从未尝试过,但这看起来很有希望:stackoverflow.com/questions/25161747/…
  • TIGUZI加入这个词到底有什么问题?
【解决方案2】:

嗯,有几件事你可以尝试:

  1. 只需使用一个 SELECT:

    select regexp_replace('home care','home care','care') as column1,
           regexp_replace('home care','home care','home') as column2,
           regexp_replace('care home','care home','home') as column3,
           regexp_replace('care home','care home','care') as column4
      from WORDLIST w
    
  2. 使用交叉连接:

    SELECT s1.COLUMN1,
           s1.COLUMN2,
           s2.COLUMN3,
           s2.COLUMN4 
      FROM (select regexp_replace('home care','home care','care') as column1,
                   regexp_replace('home care','home care','home') as column2
            from WORDLIST) s1
      CROSS JOIN (select regexp_replace('care home','care home','home') as column3,
                         regexp_replace('care home','care home','care') as column4
                    from WORDLIST) s2
    

请注意,这些查询返回的行数可能超出您的预期,具体取决于 WORDLIST 中的行数。 db<>fiddle here

您不妨考虑SELECT...FROM DUAL

【讨论】:

    猜你喜欢
    • 2017-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多