一组行的AUTO_INCREMENT列? [英] auto_increment column for a group of rows?

查看:0
本文介绍了一组行的AUTO_INCREMENT列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试如何制作一个有3列的表:

unique_id, type, version

其中UNIQUE_ID是每条记录的AUTO_INCREMENT,版本是每种类型的AUTO_INCREMENT。

目的是,当我插入时,我只需指定‘type’,系统会自动生成唯一标识和版本标识。例如:

insert type 'a', then values are:  1 , a , 1
insert type 'a', then values are:  2 , a , 2
insert type 'a', then values are:  3 , a , 3
insert type 'b', then values are:  4 , b , 1
insert type 'b', then values are:  5 , b , 2
insert type 'a', then values are:  6 , a , 4

还可以说这样的设置没有真正正常化吗?而应该是两张表?

推荐答案

不能为每种类型自动生成序列,但可以生成您自己的序列。

insert into the_table (type, version)
select 'a', 1 + IFNULL(max(version), 0)
  from the_table
  where type = 'a';

这篇关于一组行的AUTO_INCREMENT列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆