在行的末尾插入null

时间:2013-12-06 06:29:25

标签: sql database oracle plsql

如何使用select语句

在行的末尾插入null

例如,假设:

select value from (select value state from datatable where dataname like 'BENCH_DATA_S_%'')state

它将产生四行一列

RECH
MACHINE
BOOKS
ETC

现在我想在行的末尾插入null。

我想要这个输出:

RECH
MACHINE
BOOKS
ETC
null

我使用UNION尝试过,但似乎无效

4 个答案:

答案 0 :(得分:2)

如果你想让null成为最后一条记录,你可以尝试这样的事情:

  select data 
    from (select value as data,
                 1 as sort
            from datatable
           where dataname like 'BENCH_DATA_S_%''

           union all 

          select null,
                 2
            from dual)
order by sort

我已为(可能的)保留数据列的(数据)订单添加了一个额外的列(已排序); 如果数据列的顺序对您没有任何意义,您可以简化查询:

  select value as data
    from datatable
   where dataname like 'BENCH_DATA_S_%''

   union all 

  select null
    from dual

order by data asc nulls last -- or data desc nulls last

答案 1 :(得分:1)

试试这个,

SELECT value FROM(
     SELECT value FROM datatable WHERE dataname LIKE 'BENCH_DATA_S_%'
     UNION ALL
     SELECT NULL value FROM DUAL)

答案 2 :(得分:1)

如果要添加列:

SELECT RECH,
       MACHINE,
       BOOKS,
       ETC,
       null AS "null"
FROM   datatable
WHERE  dataname LIKE 'BENCH_DATA_S_%';

如果您想添加一行:

SELECT RECH,
       MACHINE,
       BOOKS,
       ETC
FROM   datatable
WHERE  dataname LIKE 'BENCH_DATA_S_%'
UNION ALL
SELECT NULL,
       NULL,
       NULL,
       NULL
FROM   DUAL
ORDER BY
       RECH NULLS LAST,
       MACHINE NULLS LAST,
       BOOKS NULLS LAST,
       ETC NULLS LAST;

答案 3 :(得分:0)

联盟应该工作。只需确保将列数与NULL数量相匹配。

SELECT 1,2,3 FROM dual
UNION ALL
SELECT NULL, NULL, NULL FROM dual
ORDER BY 1 NULLS LAST;