简体   繁体   English

将数据从同一表的其他两列插入一列

[英]Insert data into one column from two other columns of the same table

I have the following table: 我有下表:

Table A
FNAME | LNAME
james | Bond
John  | Brit
raje  | van

I want to insert first letter from first column with full last name to create a new username column for the table: 我想从第一列插入姓氏全名来为表创建一个新的用户名列:

Table A
USERNAME
jbond
jbrit
rvan

If this is not possible, I at least need to update the lastname from lname to newly created username column and to set a default password for all the rows 如果这不可能,那么我至少需要将姓氏从lname更新为新创建的用户名列,并为所有行设置默认密码

Add new column username: 添加新的列用户名:

ALTER TABLE tableA ADD username varchar(50) 
-- 50 is an example, it should be choosed depending on data

then execute this query: 然后执行以下查询:

UPDATE tableA
SET username = LEFT(fname, 1) + lname
UPDATE TableA SET username = SUBSTRING(fname,1,1) + lname

Consider using a computed column (or persisted computed column or even an indexed persisted computed column). 考虑使用计算列(或持久化计算列,甚至是索引持久化计算列)。

References: 参考文献:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将数据从一列插入另一表的两列的过程 - Procedure to insert data from one column into two columns in another table 从两列插入一列数据 - Insert into one column data from two columns sql一个表的两列引用另一表的同一列 - sql Two columns of one table references to the same column of the other table 计算表中的2列,并将结果作为同一列中的其他列插入 - Calculate the 2 columns from table and insert the result as other column in the same table 通过从两列中选择值来插入同一表中的一列 - insert by selecting values from two columns into a column from the same table 从一个表中的两个不同列中选择数据,这些列指向另一个表中的同一列 - selecting data from two different columns in one table that point to the same column in another table 如何使用Insert语句将两列或更多列数据插入一列,从一张表到另一张表 - How to insert two or more columns data into one column form one table to another table using Insert statement 单个查询根据不同的条件从同一个表中将一列的数据拆分为两列 [SQL] - Single query to split out data of one column, into two columns, from the same table based on different criteria [SQL] 从数据库中的同一列中的两列中选择一列 - Select one column from database by two columns in the same table 将表中的一个列数据插入到其他表中,但将动态指定其他列数据 - insert one column data from a table into other table, but the other column data will be specified dynamically
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM