SQL 使用 select 向查询结果添加行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/738631/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add row to query result using select
提问by Quassnoi
Is it possible to extend query results with literals like this?
是否可以使用这样的文字扩展查询结果?
select name from users
union
select name from ('JASON');
or
或者
select age, name from users
union
select age, name from (25,'Betty');
so it returns all the names in the table plus 'JASON', or (25,'Betty').
所以它返回表中的所有名字加上'JASON',或(25,'Betty')。
回答by Quassnoi
You use it like this:
你像这样使用它:
SELECT age, name
FROM users
UNION
SELECT 25 AS age, 'Betty' AS name
Use UNION ALL
to allow duplicates: if there is a 25-years old Betty among your users, the second query will not select her again with mere UNION
.
使用UNION ALL
允许重复:如果有一个25岁的贝蒂用户中,第二个查询不会再次与单纯的选择了UNION
。
回答by Mark Sherretta
In SQL Server, you would say:
在 SQL Server 中,您会说:
Select name from users
UNION [ALL]
SELECT 'JASON'
In Oracle, you would say
在 Oracle 中,您会说
Select name from user
UNION [ALL]
Select 'JASON' from DUAL
回答by Amy B
is it possible to extend query results with literals like this?
是否可以使用这样的文字扩展查询结果?
Yes.
是的。
Select Name
From Customers
UNION ALL
Select 'Jason'
- Use
UNION
to add Jason if it isn't already in the result set. - Use
UNION ALL
to add Jason whether or not he's already in the result set.
- 使用
UNION
添加贾森如果不是已经在结果集。 - 使用
UNION ALL
添加杰森不论是否是已经在结果集。