是否可以在没有表的select语句中创建许多行?

Hao*_*aru 2 mysql sql select

我想知道是否可以在此select语句中生成多于一行:

select
      floor(1+(rand()*(1+100-1))) as B1
     ,floor(1+(rand()*(1+100-1))) as B2
     ,floor(1+(rand()*(1+100-1))) as B3
     ,floor(1+(rand()*(1+100-1))) as B4
     ,floor(1+(rand()*(1+100-1))) as B5
     ,floor(1+(rand()*(1+100-1))) as B6 ;
Run Code Online (Sandbox Code Playgroud)

而不是这(单行)

    B1  B2  B3  B4  B5  B6
    -- --   --  --  --  --
    48  35  30  44  31  24
Run Code Online (Sandbox Code Playgroud)

我想看到这个(或者在循环中或者像我想要的那样多行)

B1  B2  B3  B4  B5  B6
-- --   --  --  --  --
48  35  30  44  31  24
24  2   34  15  22  15
11  7   2   36  27  26
49  19  44  17  49  47
39  4   48  32  16  34
23  10  32  29  48  9
45  49  13  17  45  25
38  16  15  25  33  41
Run Code Online (Sandbox Code Playgroud)

如果有人知道如果没有创建程序可以做到这一点我会很感激!

Mar*_*ber 5

大多数数据库提供了生成1..n行数据的方法(请参阅:SQL SELECT以获取前N个正整数)但MySQL并不容易.如果您有一个表,您知道有足够的行来满足您的要求,您可以使用它作为查询的基础,以获得您想要的.

例如,这将获得10行:

SELECT @N := @N +1 AS rownumber
     ,floor(1+(rand()*(1+100-1))) as B1
     ,floor(1+(rand()*(1+100-1))) as B2
     ,floor(1+(rand()*(1+100-1))) as B3
     ,floor(1+(rand()*(1+100-1))) as B4
     ,floor(1+(rand()*(1+100-1))) as B5
     ,floor(1+(rand()*(1+100-1))) as B6 
FROM INFORMATION_SCHEMA.COLUMNS, (SELECT @N:=0) dummyRowNums LIMIT 10;
Run Code Online (Sandbox Code Playgroud)

您可以使用任何表来实现此目的,只要您可以确定它的行数多于您希望达到的LIMIT.

如果结果集中不需要rownumber,则可以删除SELECT中的第一列,并删除连接(",(SELECT @N:= 0)dummyRowNums"):

SELECT floor(1+(rand()*(1+100-1))) as B1
     ,floor(1+(rand()*(1+100-1))) as B2
     ,floor(1+(rand()*(1+100-1))) as B3
     ,floor(1+(rand()*(1+100-1))) as B4
     ,floor(1+(rand()*(1+100-1))) as B5
     ,floor(1+(rand()*(1+100-1))) as B6 
FROM INFORMATION_SCHEMA.COLUMNS LIMIT 10;
Run Code Online (Sandbox Code Playgroud)