创建一个从两列创建一列的查询

时间:2019-05-13 10:11:18

标签: sql oracle

很抱歉,标题的描述性不足,但我真的不知道如何将其放在句子中。可以说我有这张桌子:

ID | LOC | NAMEA | NAMEB
------------------------
 0 |  BL |     X |     Y
 1 |  BG |     Z |  NULL

我想知道是否可以编写一个查询来从表中返回该查询:

ID | LOC | NAME
------------------------
 0 |  BL |    X 
 0 |  BL |    Y
 1 |  BG |    Z

我知道如果我需要使用它,那么数据库很糟糕,我不打算这样做,但是我只是想知道是否有可能以及如何做。

5 个答案:

答案 0 :(得分:1)

union allselect id, loc,nameA from tablename where nameA is not null union all select id, loc,nameB from tablename where nameB is not null )帮助:

UNION

答案 1 :(得分:1)

您可以使用此-

SELECT ID, LOC, NAMEA AS NAME
FROM your_table
WHERE NAMEA IS NOT NULL

UNION ALL

SELECT ID, LOC, NAMEB AS NAME
FROM your_table
WHERE NAMEB IS NOT NULL

答案 2 :(得分:0)

使用ValidateEntity

IEntityValidator<T> validatorObj = (T)ValidatorLocator.GetValidator(entityName, this);

答案 3 :(得分:0)

您想要ALL

SQL> with test (id, loc, namea, nameb) as
  2    (select 0, 'BL', 'X', 'Y' from dual union all
  3     select 1, 'BG', 'Z', NULL from dual
  4    )
  5  select id, loc, namea from test where namea is not null
  6  union all
  7  select id, loc, nameb from test where nameb is not null
  8  order by id;

        ID LO N
---------- -- -
         0 BL X
         0 BL Y
         1 BG Z

SQL>

答案 4 :(得分:0)

我没有直接查询返回上述输出,但是您可以尝试以下步骤。这样,您将创建一个新表,其中包含您需要的数据作为输出。 步骤1:

`create table test2 as (select id,loc,namea from test1);`

第2步:create table test3 as (select id,loc,nameb from test1);

第3步:insert into test2(id,loc,namea) select * from test3 where nameb='y';

步骤4:select * from test2

再有一个东西namea,nameb应该具有相同的数据大小。

相关问题