gpt4 book ai didi

sql - 如何将具有特定值的行 append 到我的 T-SQL 查询的输出?

转载 作者:行者123 更新时间:2023-12-02 07:23:18 24 4
gpt4 key购买 nike

我使用的是 SQL Server 2014,我有以下运行良好的 T-SQL 查询:

SELECT Id, Name from TravelAgency

此查询输出的最后一行如下所示:

Id       Name
--- ----
--- ----
715 AB Ltd

我想“手动”将以下值 append 到此查询的结果集中:(999, XY Ltd) 以便运行修改后的查询会得到以下结果:

Id       Name
--- ----
--- ----
715 AB Ltd
999 XY Ltd

为了简化,查询从 TravelAgency 表中提取请求的数据,并在输出末尾添加指定的值。我需要在查询中添加这些特定值,但我不知道该怎么做!

最佳答案

使用联合所有:

select id, name from xxx -- is your query
union all
select 999 as id, 'XY Ltd' as name

编辑:除了@ThorstenKettner 的评论:如果 ID 不是数字或出于任何原因您不能使用它进行排序,那么您可以这样做:

select ID, Name
from
(
select 1 as SpecialSort, ID, Name from xxx -- is your query
union all
select 2 as SpecialSort, 999 as ID, 'XY Ltd' as Name
) AllData
order by SpecialSort asc -- like this your manually added row will appear at the end

关于sql - 如何将具有特定值的行 append 到我的 T-SQL 查询的输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37316901/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com