[英]What's the most efficient way of truncating the names in a first_name and last_name column to 1 character?
What's the most efficient way of truncating the names in a first_name and last_name column to 1 character?将 first_name 和 last_name 列中的名称截断为 1 个字符的最有效方法是什么?
I have a mysql database that I want to hand off to a developer but I want to partially sanitize the data so that the two name records drop down to just initials.我有一个 mysql 数据库,我想将其交给开发人员,但我想对数据进行部分清理,以便将两个名称记录降为首字母。
I tried to modify the varchar to 1 character but mysql will not let me truncate the data, it just throws an error.我试图将 varchar 修改为 1 个字符,但 mysql 不会让我截断数据,它只会引发错误。 I am working from a dump of the database before I hand it off.在我交出之前,我正在从数据库的转储中工作。 I want to obscure the names without making them all the same.我想隐藏名称而不使它们完全相同。
update tablename set first_name=left(first_name, 1), last_name = left(last_name, 1)
但是,正如 Gordon Linoff 提到的,如果表中有很多行,这可能会很昂贵
Updating all the rows is rather expensive because of the logging required.由于需要日志记录,更新所有行的成本相当高。 The most efficient way is probably to create a new table:最有效的方法可能是创建一个新表:
create table for_developer as
select left(first_name, 1) as first_name, left(last_name, 1) as last_name,
. . . -- rest of columns
from t;
Why don't you try:你为什么不试试:
Update "you_table"
SET first_name = LEFT(first_name,1),
last_name = LEFT(last_name,1);
You could use like
when creating a new table so you have the same column attribute and definitions as the old table.您可以在创建新表时使用like
,这样您就拥有与旧表相同的列属性和定义。 Be sure to check the manual on the behavior of like
when creating tables though, especially if you are concerned about indexing.但是,请务必在创建表时查看有关like
行为的手册,尤其是在您担心索引的情况下。
create table new_table like old_table;
insert into new_table
select left(first_name, 1) as first_name, left(last_name, 1) as last_name, ..-- rest of columns
from old_table;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.