繁体   English   中英

将 first_name 和 last_name 列中的名称截断为 1 个字符的最有效方法是什么?

[英]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 个字符的最有效方法是什么?

我有一个 mysql 数据库,我想将其交给开发人员,但我想对数据进行部分清理,以便将两个名称记录降为首字母。

我试图将 varchar 修改为 1 个字符,但 mysql 不会让我截断数据,它只会引发错误。 在我交出之前,我正在从数据库的转储中工作。 我想隐藏名称而不使它们完全相同。

update tablename set first_name=left(first_name, 1), last_name = left(last_name, 1)

但是,正如 Gordon Linoff 提到的,如果表中有很多行,这可能会很昂贵

由于需要日志记录,更新所有行的成本相当高。 最有效的方法可能是创建一个新表:

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;

你为什么不试试:

Update "you_table"
SET first_name = LEFT(first_name,1), 
last_name = LEFT(last_name,1);

您可以在创建新表时使用like ,这样您就拥有与旧表相同的列属性和定义。 但是,请务必在创建表时查看有关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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM