打印数字1到100,每行10个数字

时间:2015-11-23 04:06:57

标签: mysql

在一小时前重新打印已删除的问题时,

  

如果我想打印数字1-100,有10个数字到一行   在mysql shell中,我该怎么做呢?

3 个答案:

答案 0 :(得分:2)

社区维基回答以免收集积分。随意编辑。

select theAnswer
from
(   select @rn:=@rn+1 as rownum,
    concat(1+(@rn-1)*10,' ',2+(@rn-1)*10,' ',3+(@rn-1)*10,' ',4+(@rn-1)*10,' ',5+(@rn-1)*10,' ',
        6+(@rn-1)*10,' ',7+(@rn-1)*10,' ',8+(@rn-1)*10,' ',9+(@rn-1)*10,' ',10+(@rn-1)*10,' ') as theAnswer
    from (select @rn:=0) params1
    cross join (select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9 union select 10) params2
) xDerived;

+---------------------------------+
| theAnswer                       |
+---------------------------------+
| 1 2 3 4 5 6 7 8 9 10            |
| 11 12 13 14 15 16 17 18 19 20   |
| 21 22 23 24 25 26 27 28 29 30   |
| 31 32 33 34 35 36 37 38 39 40   |
| 41 42 43 44 45 46 47 48 49 50   |
| 51 52 53 54 55 56 57 58 59 60   |
| 61 62 63 64 65 66 67 68 69 70   |
| 71 72 73 74 75 76 77 78 79 80   |
| 81 82 83 84 85 86 87 88 89 90   |
| 91 92 93 94 95 96 97 98 99 100  |
+---------------------------------+

from ( )内的东西是派生表,每个派生表都需要一个别名,xDerived

@rn是行号变量。它在params1派生表中初始化。一排。

params2是另一个派生表,第1行到第10行作为值。

cross join创建一个1x10的cartesian product(所有排列),这会产生10行,@rn每行都会增加。

由于我们只需要一列输出,因此外包装器只对一列进行最终选择,以避免输出行号列。

如果想在mysql中使用WHILE DO循环,可以使用存储过程。

答案 1 :(得分:1)

一般来说,我所做的是创建一个表(通常是临时表)并使用存储过程填充它。

CREATE TABLE `numTable` (
  `Id` int(11) NOT NULL auto_increment,
  PRIMARY KEY  (`Id`)
)//

CREATE PROCEDURE dowhile(IN tableLimit INT)
BEGIN
  DECLARE pointer INT DEFAULT tableLimit;
  WHILE pointer > 0 DO
    INSERT numTable VALUES (NULL);
    SET pointer = pointer - 1;
  END WHILE;
END//

CALL dowhile(100)//

现在你可能需要使用DELIMITER但是为了保持一致性我通过将Schema Delimiter设置为//(模式窗口下方的第四个按钮)来复制SQL Fiddle中的工作原理< / p>

然后从那里我通过给每一行组ID来选择这个表。由于您需要10个组,我将组设置为10的倍数,然后使用GROUP_CONCAT按此组ID进行分组以生成行。

select myRow 
from ( 
  SELECT group_concat(id SEPARATOR ', ') as `myRow`, CEIL(id/10) as `groupId`  
  FROM numTable group by `groupID`) as myTable;

SQL Fiddle

因为我们不想显示组ID我然后将其作为子选择并仅选择我的新行。如果您在PHP或C#之类的东西中使用它来输出行,您可以选择一个选择,因为您不必输出从查询结果中获得的所有内容。

答案 2 :(得分:1)

MariaDB中的sequence plugin

select group_concat(seq order by seq separator ' ')
from seq_1_to_100
group by (seq-1) div 10;

| group_concat(seq order by seq separator ' ') |
|----------------------------------------------|
| 1 2 3 4 5 6 7 8 9 10                         |
| 11 12 13 14 15 16 17 18 19 20                |
| 21 22 23 24 25 26 27 28 29 30                |
| 31 32 33 34 35 36 37 38 39 40                |
| 41 42 43 44 45 46 47 48 49 50                |
| 51 52 53 54 55 56 57 58 59 60                |
| 61 62 63 64 65 66 67 68 69 70                |
| 71 72 73 74 75 76 77 78 79 80                |
| 81 82 83 84 85 86 87 88 89 90                |
| 91 92 93 94 95 96 97 98 99 100               |

通用解决方案:

set @num_cols := 10;
set @max := 100;

select group_concat(seq order by seq separator ' ')
from seq_1_to_1000000
where seq <= @max
group by (seq-1) div @num_cols
order by min(seq);

如果你想把它们放在一个牢房中:

select group_concat(col separator '\n')
from (
    select group_concat(seq order by seq separator '\t') as col
    from seq_1_to_1000000
    where seq <= @max
    group by (seq-1) div @num_cols
) drv

想拥有专栏吗?

set @num_cols := 7;
set @num_rows := 3;

set @sql := (
    concat('select ', (
        select group_concat('(seq-1)*', @num_cols, '+', seq, ' as c', seq)
        from seq_1_to_1000000
        where seq <= @num_cols
    ),' from seq_1_to_1000000 where seq<=', @num_rows)
);

prepare stmt from @sql;
execute stmt;

| c1 | c2 | c3 | c4 | c5 | c6 | c7 |
|----|----|----|----|----|----|----|
|  1 |  2 |  3 |  4 |  5 |  6 |  7 |
|  8 |  9 | 10 | 11 | 12 | 13 | 14 |
| 15 | 16 | 17 | 18 | 19 | 20 | 21 |

如果您没有使用序列插件的MariaDB,您可以创建一个包含序列号的辅助表。问德鲁怎么做: - )