Wednesday, December 29, 2021

[FIXED] Is there an easy way to create a table with 500000 (dummy/zero/empty) rows

Issue

I want to do something that sounds so easy, but I was not able to find an easy way to solve it:
Create a new table with, let's say, 500000 rows and get them numbered automatically (ID).

Using PHP it's probably a simple loop, but I wonder if it is really necessary to use PHP.

I am almost sure there is a simple solution using a MySQL command or even with phpMyAdmin.


Solution

Join any two tables without condition and the resulting number of rows will be multiplied. Increase the id on every select from that result using a local variable.

This creates about 1M rows (2^20);

set @i = 0;
drop TEMPORARY table if exists dummyids;
create TEMPORARY table dummyids
    select @i := @i + 1 as id
    from (select true union all select true) t0
    join (select true union all select true) t1
    join (select true union all select true) t2
    join (select true union all select true) t3
    join (select true union all select true) t4
    join (select true union all select true) t5
    join (select true union all select true) t6
    join (select true union all select true) t7
    join (select true union all select true) t8
    join (select true union all select true) t9
    join (select true union all select true) t10
    join (select true union all select true) t11
    join (select true union all select true) t12
    join (select true union all select true) t13
    join (select true union all select true) t14
    join (select true union all select true) t15
    join (select true union all select true) t16
    join (select true union all select true) t17
    join (select true union all select true) t18
    join (select true union all select true) t19
;
select * from dummyids;


Answered By - Paul Spiegel

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.