我试图将列值增加1,但行号10重置,从1开始,就像这样
我喜欢600多万排
我尝试通过php调用所有600 k行并更新它们,但是我停止了,并给出了一些错误。
表样:
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行更新一次并重置一次。
谢谢
发布于 2022-03-11 16:22:55
我不太清楚您的数据是什么样子的,也不清楚何时需要重置,但是触发器可能是合适的。
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)https://stackoverflow.com/questions/71440799
复制相似问题