如何在 SQL 中将两列追加到一列中?

发布于 2024-12-14 03:26:55 字数 762 浏览 5 评论 0原文

我的表中有两列称为“工人 - 技术人员”。在工人中,我有工人的姓名,在技术人员中,我有在公司工作的技术人员的姓名。它的外观如下:

工人                         技术人员

Andy                  Kevin
Conan                 Jason
Jay                   Ray
Donald                Daryl
Martin                .
Mark                  .(rows goes on)
.
.(rows goes on)       

我想要做的是追加这些行并创建一个名为“员工”的列。这是我想要的:

Employees

Andy                  
Conan                 
Jay                   
Donald               
Martin                
Mark
Kevin
Jason
Ray
Daryl
.
.

我不想创建另一个表,我只想添加一个新列。我该怎么做?

I have two columns in my Table called as Workers - Technicians. In Workers I have name of workers, and in Technicians I have name of technicians who is working in company. Here is how it looks:

Workers                              Technicians

Andy                  Kevin
Conan                 Jason
Jay                   Ray
Donald                Daryl
Martin                .
Mark                  .(rows goes on)
.
.(rows goes on)       

What I want to do is to append these rows and having a column called Employees. Here what i want:

Employees

Andy                  
Conan                 
Jay                   
Donald               
Martin                
Mark
Kevin
Jason
Ray
Daryl
.
.

I don't want to create another table, I just want to add a new column. How would i do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

勿忘初心 2024-12-21 03:26:55

工会声明应该可以完成这里的工作。像这样的事情:

 select Name from Workers-Technitians where Worker not null
 UNION 
 select Name from Workers-Technitians where Technitian not null

请记住,使用联合时两个查询必须具有相等的列数

An union statement should do the job here. Something like this:

 select Name from Workers-Technitians where Worker not null
 UNION 
 select Name from Workers-Technitians where Technitian not null

Keep in mind that both queries must have an equal column count when using an union

怪我太投入 2024-12-21 03:26:55

假设您的表名为“staff”,您可以执行类似的操作。

select distinct employees from (
  select workers as employees from staff s1
  union select technicians as employees from staff s2
) as emps

不过,也许您需要重新审视您的设计。添加第三列对您来说不起作用,因为前两列代表两行。事实上,工人和技术人员之间似乎已经存在一种被迫的关系——他们似乎没有任何特殊原因被捆绑在同一个记录中。

考虑从 Employee 表开始,每行一名员工,并放弃现有表。如果要求很简单,您可以在该表中维护员工属性,或者对于更复杂的情况加入单独的属性表。无论哪种情况,规范化数据都会让您的生活更轻松,并且无需使用并集来连接列。

Assuming your table is named "staff", you could do something like this.

select distinct employees from (
  select workers as employees from staff s1
  union select technicians as employees from staff s2
) as emps

Perhaps you need to take another look at your design, though. Adding a third column won't work out for you because the first two columns represent two rows. In fact, there already appears to be a forced relationship between the workers and technicians-- they seem to be bundled together in the same record for no particular reason.

Consider starting fresh with an Employee table, with one employee per row, and abandon the existing table. You can maintain employee attributes in that table if the requirements are simple, or join a separate table of attributes for more complex cases. In either case, normalizing your data will make your life easier and use of a union to join columns will be unnecessary.

美人迟暮 2024-12-21 03:26:55

我也不完全理解你想要做什么(主要是因为:例如,如果一行中有“Andy”在Workers中和“Kevin”在Technicians中,您将在 Employees 中输入什么?Andy Kevin?),但要向表中添加新列,然后用您需要的数据填充它:

  1. 添加它(假设您的表名为 Table 且新列 100 个字符就足够了):

    <块引用>

    更改表 [表] 添加 [员工] nvarchar(100) null

  2. 填充:

    <块引用>

    更新[表]设置[员工] = [工人]

    -- 或:

    更新 [表] 设置 [员工] = [技术人员]

我相信您真正想要的是一个新表,Employees,并更改 Workers< /em>和技术人员从中建立关系。为此,请参阅 Simon 为您提供的有关数据库规范化的链接。

I also don't understand fully what you want to do (mainly because of this: if, say, a row has "Andy" in Workers and "Kevin" in Technicians, what will you put in Employees? Andy or Kevin?), but to add a new column to the table and then fill it with data you need to:

  1. Add it (assuming your table is named Table and 100 chars are enough for the new column):

    alter table [Table] add [Employees] nvarchar(100) null

  2. Fill it:

    update [Table] set [Employees] = [Workers]

    -- or:

    update [Table] set [Employees] = [Technicians]

I believe what you really want is a new table, Employees, and to change the Workers and Technicians into relationships drawn from it. For that, see the link Simon gave you about database normalization.

一念一轮回 2024-12-21 03:26:55

我不完全理解你的问题,但你不想添加额外的列,这会引入(更多)数据冗余,并使任何未来的更新或查询变得更加困难。使用给定的 SELECT 查询解决方案之一,并认真考虑将现有的两个单独列重构为单个列。如果您还不熟悉这个概念,我建议您看一下数据库规范化

I don't entirely understand your question but you don't want to add an additional column, this introduces (more) data redundancy and will make any future updates or querying more difficult. Use one of the SELECT query solutions given and seriously consider re-factoring your two existing separate columns into a single column. I suggest you take a look at database normalisation if you're not already familiar with the concept.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文