SQL 添加代表两个其他 Varchar 列的串联的列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24729372/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 02:12:36  来源:igfitidea点击:

Add a Column that Represents a Concatenation of Two Other Varchar Columns

sqloracleconcatenationoracle-sqldeveloper

提问by Stampin Stephie

I have an employees table and I want to add a third column valued as the concatenation of the first and last name called "FullName". How can I accomplish that without losing any data from either of the first two columns?

我有一个员工表,我想添加第三列,其值是名为“FullName”的名字和姓氏的串联。如何在不丢失前两列中的任何一列中的任何数据的情况下完成此操作?

回答by Matthew Haugen

Quick preface: this answer was based on the originally incorrect tag that this question was relating to SQL Server. I'm no longer aware of its validity on Oracle SQL Developer.

快速前言:这个答案是基于这个问题与 SQL Server 相关的最初不正确的标签。我不再知道它在 Oracle SQL Developer 上的有效性。

ALTER TABLE Employees ADD FullName AS (FirstName + ' ' + LastName)

Although in practice I'd advise that you do that operation in your SELECT. That's somewhat personal preference, but I tend to think doing things in your end queries is a bit cleaner, more readable, and easier to maintain than storing extra, calculated columns.

尽管在实践中我建议您在SELECT. 这在某种程度上是个人偏好,但我倾向于认为在最终查询中执行操作比存储额外的计算列更清晰、更具可读性且更易于维护。

Edit:

编辑:

This was eventually found as the answer, and listed by the OP as a comment on this post. The following is appropriate syntax for Oracle Sql Database.

这最终被发现作为答案,并被 OP 列为对这篇文章的评论。以下是适用于 Oracle Sql 数据库的适当语法。

ALTER TABLE emps MODIFY (FULL_NAME VARCHAR2(50) GENERATED ALWAYS AS (first_name || ' ' || last_name) VIRTUAL); 

回答by mansi

If you need fullname column all time when you select from database then you can create computed column at the time of creation of your table employee.

如果从数据库中选择时始终需要全名列,那么您可以在创建表员工时创建计算列。

for example:

例如:

CREATE TABLE Employee
(
  FirstName VARCHAR(20),
  LastName VARCHAR(20),
  FullName AS CONCAT(FirstName,' ',LastName)
)

INSERT INTO Employee VALUES ('Rocky','Jeo')

SELECT * FROM Employee 

  Output:

  FirstName  LastName  FullName
  Rocky      Jeo       Rocky Jeo

回答by Hymany

It depends on your purpose, whether you really need to add a new column to your database, or you just need to query out the "full name" on an as-needed basis.

这取决于您的目的,是真的需要向数据库添加新列,还是只需要根据需要查询“全名”。

To view it on the fly, just run the query

要即时查看,只需运行查询

SELECT firstname + ' ' + lastname AS FullName FROM employees

SELECT firstname + ' ' + lastname AS FullName FROM employees

Beyond that, you also can create a simple Stored Procedure to store it.

除此之外,您还可以创建一个简单的存储过程来存储它。

回答by Mohammed Ali

(For single result use equal to in the where condition)

(对于单个结果,在 where 条件下使用等于)

select * 
from TABLE_name 
where (Column1+Column2) in (11361+280,11365+250)