In Excel, filling series with linear step value is simple. How do I do that in MySQL?
(1) SELECT * FROM blog_posts
where postid
= 5 ORDER BY rowid
ASC
I get this query result from a huge table:
rowid postid Unix_TimeStamp
100 5 1000000000
135 5 1656885375
142 5 1885649882
208 5 1928211766
(2)Next, I I need to alter the values of Unix_TimeStamp. I want to leave the first row (rowid=100) alone, then every row's Unix_TimeStamp is 100 higher than the previous row's. The result would be:
rowid postid Unix_TimeStamp
100 5 1000000000
135 5 1000000100
142 5 1000000200
208 5 1000000300
Thanks a lot for generous replies.
In mysql 5.x you can do this like
In mysql 8 you have the window function rownumber
Schema (MySQL v5.7)
CREATE TABLE blog_posts (
`rowid` INTEGER,
`postid` INTEGER,
`Unix_TimeStamp` INTEGER
);
INSERT INTO blog_posts
(`rowid`, `postid`, `Unix_TimeStamp`)
VALUES
('100', '5', '1000000000'),
('135', '5', '1656885375'),
('142', '5', '1885649882'),
('208', '5', '1928211766');
Query #1
SELECT
`rowid`, `postid`
,(SELECT MIN(`Unix_TimeStamp`) FROM blog_posts where postid = 5 ) + @rn *100 `Unix_TimeStamp`
,@rn := @rn + 1 ronn
FROM blog_posts, (SELECT @rn := 0) a
where postid = 5
ORDER BY rowid ASC;
| rowid | postid | Unix_TimeStamp | ronn |
| ----- | ------ | -------------- | ---- |
| 100 | 5 | 1000000000 | 1 |
| 135 | 5 | 1000000100 | 2 |
| 142 | 5 | 1000000200 | 3 |
| 208 | 5 | 1000000300 | 4 |
UPDATE blog_posts bp INNER JOIN (SELECT
`rowid`, `postid`
,(SELECT MIN(`Unix_TimeStamp`) FROM blog_posts where postid = 5 ) + @rn *100 `Unix_TimeStamp`
,@rn := @rn + 1 ronn
FROM blog_posts, (SELECT @rn := 0) a
where postid = 5
ORDER BY rowid ASC) t1 ON bp.rowid = t1.rowid
[View on DB Fiddle](https://www.db-fiddle.com/f/wUqVKNZy96RjR7hTk3md7o/4)