Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sql syntax to insert every thousandth positive integer, starting from 1 and up until 1 million?

Tags:

sql

mysql

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
like image 454
user784637 Avatar asked Oct 05 '22 20:10

user784637


1 Answers

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..."

like image 113
DRapp Avatar answered Oct 09 '22 01:10

DRapp