向 SQL 查询的结果添加静态值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6472115/
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
Adding a static value to the results of an SQL query
提问by Zeke Hansell
I'm wondering if there is a way to accomplish this with an SQL query.
我想知道是否有办法通过 SQL 查询来完成此操作。
I have a table, lets call it "LISTOFTHINGS" that has two fields of interest "ID" and "NAMEOFTHING"
我有一个表,我们称之为“LISTOFTHINGS”,它有两个感兴趣的字段“ID”和“NAMEOFTHING”
What I want to do is construct a query such that what gets returned is the results of this query:
我想要做的是构造一个查询,以便返回的是此查询的结果:
SELECT ID, NAMEOFTHING FROM LISTOFTHINGS ORDER BY NAMEOFTHING
and adds a row before the first row of the above query that has " -1, 'ALL THINGs' " as the values.
并在上述查询的第一行之前添加一行,该行的值是“-1, 'ALL THINGs'”。
So if the table has the following three entries:
因此,如果该表具有以下三个条目:
1, 'THING 1'
3, 'THING 3'
2, 'THING 2'
Then the result that I want looks like this:
然后我想要的结果是这样的:
-1, 'ALL THINGS'
1, 'THING 1'
2, 'THING 2'
3, 'THING 3'
I know that I can do the query and create the list with code, but inside the VB6 program where I am using this, I have a 3rd party app (which I don't have the code for) that takes the query to populate an ACTIVEX table control with the results. I don't have the hooks to go in to add the static value.
我知道我可以执行查询并使用代码创建列表,但是在我使用它的 VB6 程序中,我有一个第 3 方应用程序(我没有代码)它需要查询来填充ACTIVEX 表控制与结果。我没有挂钩可以添加静态值。
I also know that I could just put a record in the table for " -1, 'ALL THINGS' " but the problem is, if I do that, I will need to change a lot of places in the program to ignore that record when doing processing.
我也知道我可以在表中为“-1, 'ALL THINGS'”添加一条记录,但问题是,如果我这样做,我将需要更改程序中的很多地方以忽略该记录做处理。
The 'ALL THINGS' value is sort of a pseudo recordthat handles a special case for one part of the program.
'ALL THINGS' 值是一种伪记录,用于处理程序某一部分的特殊情况。
回答by FrustratedWithFormsDesigner
Could you do a union in the query?
你能在查询中做一个联合吗?
SELECT -1 AS ID , 'ALL THINGS' AS NAMEOFTHING FROM DUAL /*'FROM DUAL' is an Oracle thing,
not sure if you need to do
something like that in DB2*/
UNION
SELECT ID, NAMEOFTHING FROM LISTOFTHINGS ORDER BY NAMEOFTHING
Apparently, this is how it should be done for DB2
显然,这就是应该为 DB2 做的事情
SELECT -1 AS ID , 'ALL THINGS' AS NAMEOFTHING FROM SYSIBM.SYSDUMMY1
UNION
SELECT ID, NAMEOFTHING FROM LISTOFTHINGS ORDER BY NAMEOFTHING
回答by Chandu
Try this:
尝试这个:
SELECT -1 AS ID, 'ALL THINGs' AS NAMEOFTHING FROM SYSIBM.SYSDUMMY1
UNION
SELECT ID, NAMEOFTHING FROM LISTOFTHINGS