选择 2 列合而为一并将它们合并
是否可以仅选择一列中的 2 列并将它们组合起来?
示例:
从 someTable 中选择某些内容 + someElse 作为 onlyOneColumn
Is it possible to select 2 columns in just one and combine them?
Example:
select something + somethingElse as onlyOneColumn from someTable
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
是的,就像您所做的那样:
如果您查询数据库,您就会得到正确的答案。
发生的事情是你要求一个表达。一个非常简单的表达式只是一个列名,更复杂的表达式可以包含公式等。
Yes, just like you did:
If you queried the database, you would have gotten the right answer.
What happens is you ask for an expression. A very simple expression is just a column name, a more complicated expression can have formulas etc in it.
是的,
将产生如下数据集:
Yes,
will result in data set like:
其他答案都不适合我,但这确实有效:
None of the other answers worked for me but this did:
是的,只要数据类型兼容,就有可能。如果不是,请使用 CONVERT() 或 CAST()
Yes it's possible, as long as the datatypes are compatible. If they aren't, use a CONVERT() or CAST()
+
运算符应该可以很好地实现这一目的。但请记住,如果其中一列为空或没有任何值,它会给您一个NULL
结果。相反,将+
与函数COALESCE
结合起来即可。下面是一个示例:
在此示例中,如果
column1
为NULL
,则将显示column2
的结果,而不是简单的空。
希望这有帮助!
The
+
operator should do the trick just fine. Keep something in mind though, if one of the columns is null or does not have any value, it will give you aNULL
result. Instead, combine+
with the functionCOALESCE
and you'll be set.Here is an example:
For this example, if
column1
isNULL
, then the results ofcolumn2
will show up, instead of a simpleNULL
.Hope this helps!
为了完成@Pete Carter的答案,我会在UNION上添加一个“ALL”(如果您需要保留重复的条目)。
(从表中选择列1作为列)
UNION ALL
(从表中选择列2作为列)
然后将返回:结果
To complete the answer of @Pete Carter, I would add an "ALL" on the UNION (if you need to keep the duplicate entries).
(SELECT column1 as column FROM table )
UNION ALL
(SELECT column2 as column FROM table )
Would then return : Result
是的,您可以轻松地组合列,例如连接字符数据:
或添加(例如)数字数据:
在这两种情况下,您最终都会得到一个列
bothcols
,其中包含组合数据。如果列不兼容,您可能必须强制数据类型。Yes, you can combine columns easily enough such as concatenating character data:
or adding (for example) numeric data:
In both those cases, you end up with a single column
bothcols
, which contains the combined data. You may have to coerce the data type if the columns are not compatible.如果其中一列是数字,我经历过,oracle 会认为“+”作为求和运算符而不是串联。
例如:
抛出无效数字异常,
在这种情况下您可以 ||运算符是串联。
if one of the column is number i have experienced the oracle will think '+' as sum operator instead concatenation.
eg:
throws invalid number exception
in such case you can || operator which is concatenation.
我希望这个答案有帮助:
I hope this answer helps:
您的语法应该有效,也许可以在列之间添加一个空格,例如
SELECT Something + ' ' + SomethingElse as onlyOneColumn FROM someTable
Your syntax should work, maybe add a space between the colums like
SELECT something + ' ' + somethingElse as onlyOneColumn FROM someTable
这里
||
是用于将它们连接到单列的 concat 运算符,而||
内的 (''
) 用于两列之间的空格。Here
||
is the concat operator used for concatenating them to single column and (''
) inside||
used for space between two columns.