首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将列值增加1并每9行重置一次

如何将列值增加1并每9行重置一次
EN

Stack Overflow用户
提问于 2022-03-11 15:22:18
回答 1查看 28关注 0票数 0

我试图将列值增加1,但行号10重置,从1开始,就像这样

我喜欢600多万排

我尝试通过php调用所有600 k行并更新它们,但是我停止了,并给出了一些错误。

表样:

代码语言:javascript
复制
CREATE TABLE `s1_table_play` (
  `id` int(11) NOT NULL,
  `player_id` int(11) DEFAULT 1,
  `play_points` varchar(20) DEFAULT '0',
  `original_points` varchar(20) DEFAULT '0',
  `play_type` varchar(20) DEFAULT '0',
  `created_at` timestamp NULL DEFAULT current_timestamp(),
  `table_id` bigint(20) DEFAULT 0,
  `play_number` int(11) DEFAULT 1
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `s1_table_play` (`id`, `player_id`, `play_points`, `original_points`, `play_type`, `created_at`, `table_id`, `play_number`) VALUES
(1, 1, '200', '0', '0', '2021-07-01 10:58:00', 67667, 1);

这个字段"play_number“需要每10行更新一次并重置一次。

谢谢

EN

回答 1

Stack Overflow用户

发布于 2022-03-11 16:22:55

我不太清楚您的数据是什么样子的,也不清楚何时需要重置,但是触发器可能是合适的。

代码语言:javascript
复制
DROP TABLE IF EXISTS T;
create table t
(id int, playno int);

drop trigger if exists t;
delimiter  $$
create trigger t before insert on t
for each row
begin
    declare n int;
    set n = (select playno from t where id < new.id order by id desc limit 1);
    set n = coalesce(n+ 1,1);
    if n >2 then set n = 1; end if;
    set new.playno = n;
end $$
delimiter ;

insert into t(id) values (1),(2),(3),(4),(5);
select * from t; 

+------+--------+
| id   | playno |
+------+--------+
|    1 |      1 |
|    2 |      2 |
|    3 |      1 |
|    4 |      2 |
|    5 |      1 |
+------+--------+
5 rows in set (0.001 sec)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71440799

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档