我需要遍历DB中的每一个,并为它们提供一个独特的3/4字母股票符号之类的代码。
我有大约600个(人的)名字,想为他们所有人生成股票符号一样的唯一代码,但大致基于名字(比如股票符号)。
例如:Dale Cooper可以是DCO/DAC/DC,但Darren Cooper应该生成不同的代码,因为所有600都应该是唯一的。
first_name和last_name位于不同的列中。
非常感谢您的建议。
它可能有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)
这样,每个名字都有1 * 26 * 1 * 26 = 676
的可能性,Dale Cooper或Darren Cooper将是676中的2个,即:
Dale Cooper DACA
Darren Cooper DACB
Douglas Cooper DACC
当第四个字母到达Z
时,第二个字母得到B
:
Dave Cooper DACZ
Doctor Cooper DBCA
等等。
编辑
为了使姓名在字母代码中更直观,您也可以使用:
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)
只使用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)
)
你得到:
- 名字的第一个字母
- 姓氏的第一个字母
- 名字+姓氏的前两个字母
你不应该为这么小的一群人发生任何冲突。