使用sql自动生成自定义id
我对 sql 和数据库比较陌生,希望在以下主题中获得一些帮助。 我有下表:(少年男性 -> jm,少年女性 -> jf,成年男性 -> am,成年女性 -> af)
id code name
1 jm john
2 am patrick
3 af jane
4 jm peter
5 jm derrick
6 af mary
7 jf jessica
并且想在表格内创建一个字段作为参考,该字段是以这种方式由代码和自动增量数字组成:
id code name reference
1 jm john jm001
2 am patrick am001
3 af jane af001
4 jm peter jm002
5 jm derrick jm003
6 af mary af002
7 jf jessica jf001
任何人都可以给我一些关于如何做到这一点的提示吗?谢谢
I am relatively new to sql and databases and would like some help in the following topic.
I have the following table: (junior male -> jm, junior female -> jf, adult male -> am, adult female-> af)
id code name
1 jm john
2 am patrick
3 af jane
4 jm peter
5 jm derrick
6 af mary
7 jf jessica
and would like to create a field inside the table as a reference which is made up of the code and autoincrement digits in this way:
id code name reference
1 jm john jm001
2 am patrick am001
3 af jane af001
4 jm peter jm002
5 jm derrick jm003
6 af mary af002
7 jf jessica jf001
can anyone give me some tips on how to do this? Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,问自己“为什么?”正如马丁的评论。如果您对这个问题有一个很好的答案,那么您可能正在寻找每种类型的序列。例如在Oracle中:
然后,您可以在输入特定类型时使用这些。例如,要输入初级男性,您可以使用:
此外,这不会像示例中那样为您提供前导零,因此如果需要,请考虑格式化您的号码。
First, ask yourself "Why?" as in Martin's comment. If you have a good answer to this question, then a sequence for each type is probably what you are looking for. For example in Oracle:
Then, you can use these when you enter the specific types. For example, to enter a junior male, you'd use:
Also, this will not give you leading zeros as in your example, so look into formatting your number if that is needed.
您可以使用以下代码来达到您想要的结果...干杯...
You can make use of this following Code in-order to arrive to your desired result... Cheers...