插入相同值后自动递增列

时间:2018-10-12 18:24:41

标签: php mysql

是否可以在插入相同的值后自动增加列,还可以检查最大为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 1 for info and appoint for table 2

有人可以帮助我实现这一目标,或者提出更好的约会安排方式吗?谢谢!

1 个答案:

答案 0 :(得分:0)

首先,这样的表设计:

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;

然后,像这样的sql:

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