查询中行的固定值

时间:2013-03-13 10:47:08

标签: sql database oracle select

考虑这个SQL:

SELECT c1 AS C1, 
       1  AS NO 
FROM   t1 
WHERE  c1 = 'abc' 
UNION 
SELECT c1 AS C1, 
       2  AS NO 
FROM   t2 
ORDER  BY NO 

正如您可能已经注意到的那样,union之前的第一个select语句只是一个旁路,用于向第二个查询返回的列表添加新值。

有没有办法在查询中不涉及t1?我觉得这是不必要的,但我不能用这个查询做更好的事情。

结果:

C1    |NO
---------------
abc   |1
def   |2
ghi   |2
jkl   |2
mno   |2
etc   |2
---------------

1 个答案:

答案 0 :(得分:5)

添加1 AS NO时,您可以添加'abc' AS C1

SELECT 'abc' AS C1, 
        1 AS NO 
   FROM DUAL
UNION 
SELECT c1 AS C1, 
       2 AS NO 
   FROM t2 
ORDER BY NO;