[英]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.