-1

I need to loop through every one in the DB and give them a unique 3/4 letter stock symbol like code.

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).

For example: Dale Cooper could be DCO/DAC/DC, but Darren Cooper should generate a different code as all 600 should be unique.

first_name and last_name are in different columns.

Advice much appreciated.

4

2 回答 2

2

为了简单起见,它可以用 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每个名字的可能性,戴尔库珀或达伦库珀将是这 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)
于 2012-08-31T11:47:10.300 回答
0

只使用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)
)

你得到:

  • 名字的第一个字母;
  • 姓氏的第一个字母;
  • 名字+姓氏的前2个字母;

对于这么少的一群人,你不应该有任何冲突。

于 2012-08-31T11:53:36.380 回答