Can I run a loop in MySQL without using a procedure/function?

后端 未结 4 935
Happy的楠姐
Happy的楠姐 2021-02-18 13:39

For testing, is it possible to run a loop from MySQL workbench or similar tool? I tried but got an error.

If it is possible, please supply a simple example I can run.

4条回答
  •  囚心锁ツ
    2021-02-18 14:30

    There's a trick with limited use-cases that is "loop-like".

    I wanted to create a large (1~2 million) row table of random integers for a test:

    INSERT INTO test_table (num) VALUES(ROUND(RAND() * 1E6));
    
    -- calling this will insert once for every row in test_table
    INSERT INTO test_table (num)
    SELECT ROUND(RAND() * 1E6)
    FROM test_table;
    

    So I quickly just kept doubling the number of rows until I had what I needed.

提交回复
热议问题