如何在 SQL 中将两列追加到一列中?
我的表中有两列称为“工人 - 技术人员”。在工人中,我有工人的姓名,在技术人员中,我有在公司工作的技术人员的姓名。它的外观如下:
工人 技术人员
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
工会声明应该可以完成这里的工作。像这样的事情:
请记住,使用联合时两个查询必须具有相等的列数
An union statement should do the job here. Something like this:
Keep in mind that both queries must have an equal column count when using an union
假设您的表名为“staff”,您可以执行类似的操作。
不过,也许您需要重新审视您的设计。添加第三列对您来说不起作用,因为前两列代表两行。事实上,工人和技术人员之间似乎已经存在一种被迫的关系——他们似乎没有任何特殊原因被捆绑在同一个记录中。
考虑从 Employee 表开始,每行一名员工,并放弃现有表。如果要求很简单,您可以在该表中维护员工属性,或者对于更复杂的情况加入单独的属性表。无论哪种情况,规范化数据都会让您的生活更轻松,并且无需使用并集来连接列。
Assuming your table is named "staff", you could do something like this.
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.
我也不完全理解你想要做什么(主要是因为:例如,如果一行中有“Andy”在Workers中和“Kevin”在Technicians中,您将在 Employees 中输入什么?Andy 或 Kevin?),但要向表中添加新列,然后用您需要的数据填充它:
添加它(假设您的表名为 Table 且新列 100 个字符就足够了):
<块引用>
更改表 [表] 添加 [员工] nvarchar(100) null
填充:
<块引用>
更新[表]设置[员工] = [工人]
-- 或:
更新 [表] 设置 [员工] = [技术人员]
我相信您真正想要的是一个新表,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:
Add it (assuming your table is named Table and 100 chars are enough for the new column):
Fill it:
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.
我不完全理解你的问题,但你不想添加额外的列,这会引入(更多)数据冗余,并使任何未来的更新或查询变得更加困难。使用给定的 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.