I have the following table:
CREATE TABLE dummy (
thousand INT(10) UNSIGNED,
UNIQUE(thousand)
);
Is there sql syntax I can use to insert every thousandth positive integer, starting from 1 and up until 1 million? I can achieve this in php, but I was wondering if this was possible without using a stored procedure.
thousand
1
1001
2001
3001
4001
...
998001
999001
insert into dummy
( thousand )
select
PreQuery.thousand
from
( select
@sqlvar thousand,
@sqlvar := @sqlvar + 1000
from
AnyTableWithAtLeast1000Records,
( select @sqlvar := 1 ) sqlvars
limit 1000 ) PreQuery
You can insert from a select statement. Using MySQL Variables, start with 1. Then, join to ANY table in your system that may have 1000 (or more) records just to generate a row. Even though not getting any actual column from such table, we just need it for the record position. Then the @sqlvar starts at 1 and is returned in column named thousand. Then, immediately add 1000 to it for the next record in the "AnyTable..."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With