简体   繁体   English

如何根据名字和姓氏生成唯一的3或4个字母的字母字符串

[英]How to generate unique 3 or 4 letter alpha strings based on first & last name

I need to loop through every one in the DB and give them a unique 3/4 letter stock symbol like code. 我需要遍历数据库中的每个人,并给他们一个独特的3/4字母股票符号,如代码。

I've got roughly 600 names (of people) and want to generate stock symbol like unique codes for them all, but based roughly on the names (like stock symbols). 我大约有600个(人)的名字,想要为他们所有人生成像唯一代码一样的股票代号,但大致基于这些名称(比如股票代号)。

For example: Dale Cooper could be DCO/DAC/DC, but Darren Cooper should generate a different code as all 600 should be unique. 例如:Dale Cooper可以是DCO / DAC / DC,但是Darren Cooper应该生成不同的代码,因为所有600应该都是唯一的。

first_name and last_name are in different columns. first_name和last_name在不同的列中。

Advice much appreciated. 忠告不胜感激。

Its possible with 4 letters, to keep it simple: 为了简单起见,可以使用4个字母:

First letter:  first letter of firstname
Second letter: letter of the alphabet (starting with `A` keeping count)
Third letter:  first letter of lastname
Fourth letter: letter of the alphabet (starting with `A` keeping count)

That way you have 1 * 26 * 1 * 26 = 676 possibilities per name, either Dale Cooper or Darren Cooper would be 2 of that 676, ie: 这样,您每个名称就有1 * 26 * 1 * 26 = 676可能性,Dale Cooper或Darren Cooper将是该676种中的2种,即:

Dale Cooper    DACA
Darren Cooper  DACB
Douglas Cooper DACC

When the fourth letter reaches Z , the second letter gets B : 当第四个字母到达Z ,第二个字母变为B

Dave Cooper    DACZ
Doctor Cooper  DBCA

Etc. 等等。

EDIT 编辑

To keep the names more visual in the lettercode you could also use: 为了使名称在字母代码中更直观,您还可以使用:

First letter:  first letter of firstname
Second letter: first letter of lastname
Third letter:  letter of the alphabet (starting with `A` keeping count)
Fourth letter: letter of the alphabet (starting with `A` keeping count)

Sticking just with mysql: 坚持使用mysql:

UPDATE people
SET code = CONCAT(
    SUBSTRING(first_name, 1, 1),
    SUBSTRING(last_name, 1, 1),
    SUBSTRING( MD5( CONCAT(first_name, last_name) ), 1, 2)
)

You get: 你得到:

  • first letter of the first name; 名字的首字母;
  • first letter of the last name; 姓氏的第一个字母;
  • first 2 letters of the has of first name + last name; 名字+姓氏的前2个字母;

You shouldn't have any collisions for such a small set of people. 对于这么少的一群人,您不应该发生任何冲突。

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

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