从查询块中随机选择一个数据

时间:2014-05-29 19:57:45

标签: php mysql sql

我在表格中有100000条记录。我需要创建一个读取10条记录的查询,然后再连续10条记录直到表的末尾。对于10行组中的每一组,我需要选择一个随机行。是否可以使用MySQL查询来实现?我需要一些想法来做到这一点。有谁能够帮我?

我试过做一个php循环,但它没有用。

<?php
include_once ("connection.php");

$data = mysql_query("SELECT * FROM trying");
$result = array();


while ($data2 = mysql_fetch_array($data))
{
    array_push($result, array('no'=> $data2['no'],
                      'source'=> $data2['source'],
                 'destination'=> $data2['destination']));
}

$e=0;   
for ($a = 0; $a <= 49;)
{

    for ($i = 0; $i <= 9; $i++,$a++) {
        $rand = array();
        $rand[$i] = $result[$a];


    }
    echo json_encode($rand[1]);

}

?>

5 个答案:

答案 0 :(得分:0)

而不是:

for ($a = 0; $a <= 49;)
{

    for ($i = 0; $i <= 9; $i++,$a++) {
        $rand = array();
        $rand[$i] = $result[$a];


    }
    echo json_encode($rand[1]);

}

你可以用这个:

$rand = array();
$step = 10;

for ($min = 0; $min <= 49; $min = $min + $step)
{   
    $max = $min + $step;
    $rand[] = $result[rand($min,$max)];
}
echo json_encode($rand);

答案 1 :(得分:0)

由于你正在回显一个json编码的数组,我假设你是通过一个AJAX请求调用这个php文件,并且你想要每10行的后续请求。

如果是这种情况,可以解决您的问题:

  1. 将限制和偏移定义为javascript中的变量
  2. 使用AJAX请求传递限制和偏移
  3. 如果AJAX请求的结果不为空,则增加偏移量(offset = offset + limit)以在下一个请求中使用
  4. 接收PHP文件中的限制和偏移量(使用$ _GET或$ _POST,取决于请求的类型)
  5. 在MySQL查询中包含限制和偏移量(“SELECT * FROM尝试LIMIT $ offset,$ limit”)
  6. 计算0到9之间的随机数(rand(0,9))
  7. 从MySQL结果中获取第n(rand)行(参见下面的解决方案)
  8. 您的PHP文件应如下所示:

    include_once ("connection.php");
    $limit = mysql_real_escape_string($_GET['limit']); // If post use $_POST
    $offset = mysql_real_escape_string($_GET['offset']); // If post use $_POST
    $data = mysql_query("SELECT * FROM trying LIMIT $offset, $limit");
    $rand = rand(0, 9);
    $count = 0;
    while($row= mysql_fetch_array($data)) {
        if($rand == $count++) {
            echo json_encode($row);
            break;
        }
    }
    

答案 2 :(得分:0)

将SQL语句放在循环中并将计数器用作限制中的变量:     

$count = mysql_query("SELECT * FROM trying");
$total = round(mysql_num_rows($count) / 10);

for ($i=0;$i<$total;$i++) {
$data = mysql_query("SELECT * FROM trying LIMIT ".($i * 10).", 10");
$result = array();


while ($data2 = mysql_fetch_array($data))
{
    array_push($result, array('no'=> $data2['no'],
                      'source'=> $data2['source'],
                 'destination'=> $data2['destination']));
}
}

$e=0;   
for ($a = 0; $a <= 49;)
{

for ($i = 0; $i <= 9; $i++,$a++) {
    $rand = array();
    $rand[$i] = $result[$a];


}
echo json_encode($rand[1]);

}


?>

答案 3 :(得分:0)

试试这个:

<?php
set_time_limit(0);
include_once ("connection.php");

$per = 10;
$start = 0;
$total = 0;

$total_query = mysql_query("SELECT COUNT(*) AS total FROM trying");
if(mysql_num_rows($total_query) == 1) {
    $row = mysql_fetch_assoc($total_query);

    $total = $row['total'];

    $offset = ($start * $per);

    $results = array();
    while($offset <= $total) {
        $tmp = array();

        $data_query = mysql_query("SELECT * FROM trying LIMIT ".$offset.",".$per);

        while ($row = mysql_fetch_array($data_query)) {
            array_push($tmp, array(
                'no'=> $row['no'],
                'source'=> $row['source'],
                'destination'=> $row['destination'])
            );
        }

        array_push($results,$tmp[rand(0,9)]);
        unset($tmp);

        $start++;
        $offset = ($start * $per);
    }

    echo json_encode($results);
} else {
    die("No records found.");
}
?>

答案 4 :(得分:0)

尝试以下方法:

<?php
include_once ("connection.php");

$query = "SELECT * FROM trying";
$count = 0;

while(true)
{

    $data = mysql_query($query." LIMIT ".$count.",10");

    $random = rand(1,10);
    for($i=1;$i<=$random;$i++)
    {
       $row =  mysql_fetch_array($data);
        if($row === false)
        {
            break 2;
        }
    }

    echo json_encode($row);

    $count++;
}

?>