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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 01:41:10  来源:igfitidea点击:

Add row to query result using select

sqlsql-servertsqlselectinsert

提问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 ALLto 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 UNIONto add Jason if it isn't already in the result set.
  • Use UNION ALLto add Jason whether or not he's already in the result set.
  • 使用UNION添加贾森如果不是已经在结果集。
  • 使用UNION ALL添加杰森不论是否是已经在结果集。