SQL:是否可以在 select 语句中添加一个虚拟列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4556397/
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
SQL: Is it possible to add a dummy column in a select statement?
提问by Kev
I need to add a dummy column to a simple select statement in certain circumstances:
在某些情况下,我需要在简单的 select 语句中添加一个虚拟列:
Select Id, EndOfcol default '~' from Main where id > 40
Select Id, EndOfcol default '~' from Main where id > 40
回答by bobs
Yes, it's actually a constant value.
是的,它实际上是一个常数值。
SELECT id, '~' AS EndOfcol
FROM Main
WHERE id > 40
回答by HLGEM
Sometimes you may want to cast the datatype of the constant especially if you plan to add other data to it later:
有时您可能想要转换常量的数据类型,特别是如果您打算稍后向其中添加其他数据:
SELECT id, cast('~' as varchar(20)) AS EndOfcol FROM Main WHERE id > 40
This is especially useful if you want to add a NULL column and then later figure out the information that goes into it as NULL will be cast as int automatically.
如果您想添加一个 NULL 列,然后再找出其中的信息,因为 NULL 将自动转换为 int,这尤其有用。
SELECT id, cast(NULL as varchar(20)) AS Myfield FROM Main WHERE id > 40
回答by shankhan
Yes, it is possible it can be constant or can be conditional
是的,它可能是恒定的,也可能是有条件的
SELECT id, '~' EndOfcol FROM Main WHERE id > 40
回答by steve
An easy solution is to add a column like this:
一个简单的解决方案是添加一个这样的列:
Select Id, EndOfcol default '~', space(2) as Dummy from Main where id > 40