MySQL插入随机项目多达10,000次?

时间:2016-05-04 14:47:27

标签: php mysql

不确定这是怎么可能的,或者它是一个简单的解决方案我不确定甚至是它的名称。

我希望将10,000行插入到数据库中,其中包含三列,用于" map"面积100x100:

-----------------
| x | y | type |
-----------------
| 1 | 1 |    1 |
-----------------
| 1 | 2 |    1 |
-----------------
| 1 | 3 |    2 |
-----------------
| 1 | 4 |    3 |
-----------------
| 1 | 5 |    4 |
-----------------
| 1 | 6 |    4 |
-----------------
| 1 | 7 |    2 |
-----------------
| 1 | 8 |    2 |
-----------------
| 1 | 9 |    1 |
-----------------
| 2 | 1 |    2 |
-----------------
| 2 | 2 |    1 |

基本上X列上升到100 Y列上升到100因此会有X:1 Y:1和X:100 Y:100以及所有变量(10,000)除此之外,类型列需要是1到4之间的随机数。(如果我可以设置每个数字的百分比,这将是理想的,例如10,000 2中的1 25%是15%,3是40%,4是20%)。

有没有办法用MySQL来生成这个,而不是为10,000行写入插入?或者我最好还是编写某种形式的PHP脚本来执行并插入它?

谢谢!

3 个答案:

答案 0 :(得分:1)

<?php
$values = [];

for ($x = 1; $x <= 100; $x++)
{
    for ($y = 1; $y <= 100; $y++)
    {
        $rand = mt_rand(1, 100);

        switch (true)
        {
            case ($rand <= 25):  $i = 1; break;
            case ($rand <= 40):  $i = 2; break;
            case ($rand <= 80):  $i = 3; break;
            case ($rand <= 100): $i = 4; break;
        }

        $values[] = sprintf("(%d, %d, %d)", intval($x), intval($y), intval($i));
    }
}

$sql = "Insert Into table (x, y, type) Values" . implode(',', $values);

echo $sql;

答案 1 :(得分:0)

我会用PHP脚本解决这个问题。

首先,您可以创建2500个1s,1500个2s,4000个3s和2000个4s的数组。然后使用两个foreach循环(对于X和Y)并从数组中的随机索引中取一个数字,从中删除元素。

你想要代码吗?

<?php
$values = array_fill(0, 2500, 1)
        + array_fill(2500, 1500, 2)
        + array_fill(4000, 4000, 3)
        + array_fill(8000, 2000, 4);

$statements = [];
foreach (range(1, 100) as $x) {
    foreach (range(1, 100) as $y) {
        $v = $values[mt_rand(0, count($values) - 1)];
        $statements[] = "INSERT INTO table (x, y, type) values ($x, $y, $v);";
    }
}

echo implode("\n", $statements);

答案 2 :(得分:0)

或者这个。你可以用nr完成一切。 INSERT INTO newtab .....

SELECT a.a*10000 + b.a*1000 + c.a*100 + d.a*10 + e.a AS nr FROM (
 SELECT 0  a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9 ) AS a
 CROSS JOIN (
 SELECT 0  a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9
) AS b
 CROSS JOIN (
 SELECT 0  a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9
) AS c
 CROSS JOIN (
 SELECT 0  a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9
) AS d
 CROSS JOIN (
 SELECT 0  a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9
) AS e
ORDER BY nr;