如何在MySQL中插入xxxxx编号的所有排列?

时间:2015-11-19 13:03:31

标签: mysql permutation

我有一张名为"数字"

的表格

id(int,auto-increment)

number(varchar)

如何插入包含5位[0-9]的数字的所有排列?

00000 00001 00002 ... 99999

2 个答案:

答案 0 :(得分:1)

example - 返回字符串str,左边填充字符串padstr为长度为len个字符。如果str长于len,则返回值将缩短为len个字符。

N/A

MySQL文档。 12.5字符串函数。 function LPAD

答案 1 :(得分:1)

这是一个select语句,生成从099999的数字,包括0左边填充。然后,您可以将其与INSERT语句一起使用,将它们一次性插入到您想要的位置。

SELECT LPAD(F.t*10000+E.num3,5,'0') num FROM
(
    SELECT 100*num1+num2 num3
    FROM (
    SELECT t*10+u num1
    FROM
        (SELECT 0 t UNION 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) A,
        (SELECT 0 u UNION 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) B
    ) C,
    (
    SELECT t*10+u num2
    FROM
        (SELECT 0 t UNION 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) A,
        (SELECT 0 u UNION 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) B
    ) D
) E,
(SELECT 0 t UNION 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) F

ORDER BY num

输出是:

+-------+
|  num  |
+-------+
| 00000 |
| 00001 |
| 00002 |
| ...   |
| 99999 |
+-------+