选择 2 列合而为一并将它们合并

发布于 2024-10-23 13:54:06 字数 113 浏览 1 评论 0原文

是否可以仅选择一列中的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

原野 2024-10-30 13:54:06
(SELECT column1 as column FROM table )
UNION 
(SELECT column2 as column FROM table )
(SELECT column1 as column FROM table )
UNION 
(SELECT column2 as column FROM table )
女皇必胜 2024-10-30 13:54:06

是的,就像您所做的那样:

select something + somethingElse as onlyOneColumn from someTable

如果您查询数据库,您就会得到正确的答案。

发生的事情是你要求一个表达。一个非常简单的表达式只是一个列名,更复杂的表达式可以包含公式等。

Yes, just like you did:

select something + somethingElse as onlyOneColumn from someTable

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.

萌辣 2024-10-30 13:54:06

是的,

SELECT CONCAT(field1, field2) AS WHOLENAME FROM TABLE
WHERE ...

将产生如下数据集:

WHOLENAME
field1field2

Yes,

SELECT CONCAT(field1, field2) AS WHOLENAME FROM TABLE
WHERE ...

will result in data set like:

WHOLENAME
field1field2
尐偏执 2024-10-30 13:54:06

其他答案都不适合我,但这确实有效:

SELECT CONCAT(Cust_First, ' ', Cust_Last) AS CustName FROM customer

None of the other answers worked for me but this did:

SELECT CONCAT(Cust_First, ' ', Cust_Last) AS CustName FROM customer
小猫一只 2024-10-30 13:54:06

是的,只要数据类型兼容,就有可能。如果不是,请使用 CONVERT() 或 CAST()

SELECT firstname + ' ' + lastname AS name FROM customers

Yes it's possible, as long as the datatypes are compatible. If they aren't, use a CONVERT() or CAST()

SELECT firstname + ' ' + lastname AS name FROM customers
柏林苍穹下 2024-10-30 13:54:06

+ 运算符应该可以很好地实现这一目的。但请记住,如果其中一列为空或没有任何值,它会给您一个 NULL 结果。相反,将 + 与函数 COALESCE 结合起来即可。

下面是一个示例:

SELECT COALESCE(column1,'') + COALESCE(column2,'') FROM table1. 

在此示例中,如果 column1NULL,则将显示 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 a NULL result. Instead, combine + with the function COALESCE and you'll be set.

Here is an example:

SELECT COALESCE(column1,'') + COALESCE(column2,'') FROM table1. 

For this example, if column1 is NULL, then the results of column2 will show up, instead of a simple NULL.

Hope this helps!

执笔绘流年 2024-10-30 13:54:06

为了完成@Pete Carter的答案,我会在UNION上添加一个“ALL”(如果您需要保留重复的条目)。

(从表中选择列1作为列)

UNION ALL

(从表中选择列2作为列)

 DROP TABLE IF EXISTS #9
    CREATE TABLE #9 
(
    USER1 int
    ,USER2 int
)
INSERT INTO #9
 VALUES(1, 2), (1, 3), (1, 4), (2, 3)
 ------------------------------------------------
(SELECT USER1 AS 'column' from #9)
 UNION ALL
 (SELECT USER2 AS 'column' from #9)

然后将返回:结果

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 )

 DROP TABLE IF EXISTS #9
    CREATE TABLE #9 
(
    USER1 int
    ,USER2 int
)
INSERT INTO #9
 VALUES(1, 2), (1, 3), (1, 4), (2, 3)
 ------------------------------------------------
(SELECT USER1 AS 'column' from #9)
 UNION ALL
 (SELECT USER2 AS 'column' from #9)

Would then return : Result

榆西 2024-10-30 13:54:06

是的,您可以轻松地组合列,例如连接字符数据:

select col1 | col 2 as bothcols from tbl ...

或添加(例如)数字数据:

select col1 + col2 as bothcols from tbl ...

在这两种情况下,您最终都会得到一个列 bothcols,其中包含组合数据。如果列不兼容,您可能必须强制数据类型。

Yes, you can combine columns easily enough such as concatenating character data:

select col1 | col 2 as bothcols from tbl ...

or adding (for example) numeric data:

select col1 + col2 as bothcols from tbl ...

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.

冰火雁神 2024-10-30 13:54:06

如果其中一列是数字,我经历过,oracle 会认为“+”作为求和运算符而不是串联。

例如:

select (id + name) as one from table 1; (id is numeric) 

抛出无效数字异常,

在这种情况下您可以 ||运算符是串联。

select (id || name) as one from table 1;

if one of the column is number i have experienced the oracle will think '+' as sum operator instead concatenation.

eg:

select (id + name) as one from table 1; (id is numeric) 

throws invalid number exception

in such case you can || operator which is concatenation.

select (id || name) as one from table 1;
初相遇 2024-10-30 13:54:06

我希望这个答案有帮助:

SELECT (CAST(id AS NVARCHAR)+','+name) AS COMBINED_COLUMN FROM TABLENAME;

I hope this answer helps:

SELECT (CAST(id AS NVARCHAR)+','+name) AS COMBINED_COLUMN FROM TABLENAME;
风筝有风,海豚有海 2024-10-30 13:54:06

您的语法应该有效,也许可以在列之间添加一个空格,例如

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

季末如歌 2024-10-30 13:54:06
select column1 || ' ' || column2 as whole_name FROM tablename;

这里 || 是用于将它们连接到单列的 concat 运算符,而 || 内的 ('') 用于两列之间的空格。

select column1 || ' ' || column2 as whole_name FROM tablename;

Here || is the concat operator used for concatenating them to single column and ('') inside || used for space between two columns.

≈。彩虹 2024-10-30 13:54:06
SELECT firstname || ' ' || lastname FROM users;
SELECT firstname || ' ' || lastname FROM users;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文