0

我的数据库中有一个列,int(11)有时我无法将用户名与 ids 数据库匹配,因为名称略有不同(即 John Doe 而不是 Johnathan Doe)。普通 ID 从 11000 范围开始,所以我想将他们的名字转换为整数并在前面加上 00 或其他东西,这样我就可以触发一个标志供某人查看和更新​​。我知道我可以用它str_pad来获取 00,但我似乎无法生成一定数量的数字以使其名称有些独特。我知道重复的可能性很高,但我不确定我还能用primary/unique. 任何帮助将不胜感激。

mt_rand使用and 的示例str_pad

str_pad(mt_rand(100000000,999999999),11,'00',STR_PAD_LEFT);

任何人都有更好的选择,可能使用名称字符串?

4

1 回答 1

1

I have a column in my database that is a int(11)

That column's an integer. At the storage level, the "11" doesn't mean a thing.

Imagine that you have a large table of user names, and somebody tries to enter 'John Smith' for the 40th time. What do you want to do? Do you want somebody to compare it to all the other variants? Do you want to identify and review variant spellings, like 'Johnathan', 'Jonathon', 'John', and 'Jhon'? Big decisions, but they don't affect how to handle id numbers that need to be reviewed.

To identify rows that need to be reviewed, for whatever reason, you're far better off putting their keys into another table.

create table review_sets (
  set_id integer not null,
  set_created timestamp not null default now(),
  rationale varchar(100) not null default 'Duplicate of existing user?',
  primary key (set_id),
  unique (set_created)
);

create table review_ids (
  set_id integer not null,
  user_id integer not null,
  primary key (set_id, user_id),
  foreign key (set_id) references review_sets (set_id),
  foreign key (user_id) references users (user_id)         -- Not shown.
);

The column review_sets.rationale lets you use distinguish spelling issues from, say, late payments or site abuse. The table of review_ids lets you limit the ids to a few variations rather than all possible variations. (But I'm not sure how useful that might be.)

You might want to add columns that identify the reviewer, the time the review was completed, and the decision reached by that reviewer. Depending on your application, you might be able to just delete rows after you've reached a decision.

于 2013-08-27T17:39:25.953 回答