插入相同值后自动递增列

ahy6op9u  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(323)

是否可以在插入相同值后自动递增列,并检查最大值为2的重复值,如下所示:

INSERT INTO info (name, date, sched) VALUES ('$name', '$date', '$sched')
// I want to auto increment for count column after inserting same date and sched
INSERT INTO appointment (date, sched) VALUES ('$date', '$sched')

这是我的table:

有人能帮我做到这一点,或者建议一个更好的预约安排方式吗?谢谢!

w6lpcovy

w6lpcovy1#

首先,table设计如下:

create table appointment (
    id int(11) unsigned not null auto_increment,
    date date not null '2018-01-01',
    sched varchar(20) not null defalut '',
    count int(11) not null default 0,
    primary key (id)
    unique key `date_sched` (`date`, `sched`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

然后,像这样:

insert into appointment(date, sched, count) values ($date, $sched, $count) 
on duplicate key update count = count + 1

相关问题