如何在 sql 查询中创建空白/硬编码列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5185743/
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
How can I create a blank/hardcoded column in a sql query?
提问by tylercomp
I want have a query with a column that is a hardcoded value not from a table, can this be done? I need it basically as a placeholder that I am going to come back to later and fill in.
我想要一个查询,其中的列是硬编码值而不是来自表,可以这样做吗?我基本上需要它作为占位符,我稍后会回来填写。
example:
例子:
SELECT
hat,
shoe,
boat,
somevalue = 0 as placeholder
FROM
objects
then I would loop through this query later and fill in the placeholder
然后我稍后会遍历这个查询并填写占位符
in this example someValue is not a field in objects, I need to fake it. I am doing this in coldfusion and using two datasources to complete one query. I have tried the space() function but have been unable to get it to work.
在这个例子中 someValue 不是对象中的字段,我需要伪造它。我在 Coldfusion 中这样做并使用两个数据源来完成一个查询。我已经尝试了 space() 函数,但一直无法让它工作。
Thanks.
谢谢。
回答by Galz
SELECT
hat,
shoe,
boat,
0 as placeholder
FROM
objects
And '' as placeholder
for strings.
而'' as placeholder
对于字符串。
回答by g.d.d.c
This should work on most databases. You can also select a blank string as your extra column like so:
这应该适用于大多数数据库。您还可以选择一个空白字符串作为您的额外列,如下所示:
Select
Hat, Show, Boat, '' as SomeValue
From
Objects
回答by Benjamin Sternlieb
For varchars, you may need to do something like this:
对于 varchars,您可能需要执行以下操作:
select convert(varchar(25), NULL) as abc_column into xyz_table
If you try
如果你试试
select '' as abc_column into xyz_table
you may get errors related to truncation, or an issue with null values, once you populate.
填充后,您可能会遇到与截断相关的错误或空值问题。
回答by charliegriefer
The answers above are correct, and what I'd consider the "best" answers. But just to be as complete as possible, you can also do this directly in CF using queryAddColumn.
上面的答案是正确的,我认为是“最佳”答案。但为了尽可能完整,您也可以使用 queryAddColumn 在 CF 中直接执行此操作。
See http://www.cfquickdocs.com/cf9/#queryaddcolumn
请参阅http://www.cfquickdocs.com/cf9/#queryaddcolumn
Again, it's more efficient to do it at the database level... but it's good to be aware of as many alternatives as possible (IMO, of course) :)
同样,在数据库级别执行此操作更有效......但最好了解尽可能多的替代方案(当然是 IMO):)
回答by Shiraj Momin
SELECT
hat,
shoe,
boat,
0 as placeholder -- for column having 0 value
FROM
objects
--OR '' as Placeholder -- for blank column
--OR NULL as Placeholder -- for column having null value
回答by mapping dom
Thank you, in PostgreSQL this works for boolean
谢谢,在 PostgreSQL 中这适用于 boolean
SELECT
hat,
shoe,
boat,
false as placeholder
FROM
objects