简体   繁体   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?

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.

相关问题 通过First_Name和/或Last_Name查询sql表 - query sql table By First_Name and/or Last_Name 更新名称后,数据库中的“ first_name”显示为0,last_name显示为“ Arora” - into my database `first_name` is showing as 0 and last_name is showing as “Arora” after updating name 获取所有具有相同first_name的用户last_name - Get all users last_name which have same first_name 从指向同一个表的两个关系中过滤 first_name 和 last_name - filter first_name and last_name from two relation pointing to same table PHP-如何在登录后获取正确的名字和姓氏? - PHP - How to get correct first_name and last_name after login? 查询以使用外键获取行值(first_name 和 last_name) - Query to fetch row values (first_name & last_name) using foreign key 搜索带有尾随通配符的first_name和last_name的最佳索引? - Best index for searching both first_name and last_name with trailing wildcards? 我正在向服务器发出 Ajax post 请求,所有数据都很好,但我的 first_name 和 last_name 被更新为“未定义” - I am doing a Ajax post request to the server all the data is fine but my first_name and last_name are being updated as "undefined" 如何编写查询以获取订单包含超过 2 件商品的客户的名字和姓氏? - How do I write a query to get the first_name and last_name of customers having orders containing more than 2 items? Smarty中的列“ first_name”不能为空 - Column 'first_name' cannot be null in smarty
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM