2

我有一个表(MySql),其中一列是-3到3之间的“值”。我想要做的是:

  1. 如果它的“值”是正数(在我的情况下为 1 到 3 - 负值将用于其他内容并且与此处无关),则从表中拉出一个随机行
  2. 如果“值”为 3,则仅显示该行。
  3. 如果“值”为 2,则显示该行,加上另一个“值”为 1 的随机行。
  4. 如果“值”为 1,则显示该行,再加上“值”为 2 的多行,或每行“值”为 1 的 2 行。

或者,换句话说,我需要列出行,直到它们的总值为 3 - 通过值为 3 的一行、值为 1 和 2(或 2 和 1)的两行或三行完成每个值为 1。

这是我所拥有的:

$query = "SELECT * FROM $usertable WHERE Value>0 ORDER BY RAND() LIMIT 1"; //from all positive Values, get one at random
$query2 = "SELECT * FROM $usertable WHERE Value BETWEEN 1 AND 2 ORDER BY RAND() LIMIT 1"; //from all Values between 1 and 2, get one at random
$query3 = "SELECT * FROM $usertable WHERE Value=1 ORDER BY RAND() LIMIT 1"; //from all Values equal to 1, get one at random
$result = mysql_query($query);
$result2 = mysql_query($query2);
$result3 = mysql_query($query3);

while($row = mysql_fetch_array($result)) 
    {
  echo $row['A'] . " " . $row['B'] . " " . $row['C'] . " " . $row['Value'];
  echo "<br>";
    }
while($row['Value']<3) 
    {
   while($row2 = mysql_fetch_array($result2)) 
        {
    echo $row2['A'] . " " . $row2['B'] . " " . $row2['C'] . " " . $row2['Value'];
    echo "<br>";
        }
    }
while($row['Value'] + $row2['Value']<3)
    {
    while($row3 = mysql_fetch_array($result3)) 
        {
        echo $row3['A'] . " " . $row3['B'] . " " . $row3['C'] . " " . $row3['Value'];
        echo "<br>";
        }
    }

我尝试将 while 语句按不同的顺序排列,但我不断收到意外的 T_WHILE 错误。

我现在拥有它的方式,代码将运行并超时并出现以下错误:“致命错误:超过 30 秒的最大执行时间”并且它没有做我需要的事情(如果第一个,它仍然会提取结果“价值”是 3)。

谁能指出我做错了什么?必须有比我正在做的更好的方法来完成这个场景,但我只是没有看到它。有人可以指出我正确的方式吗?谢谢!

4

3 回答 3

2

您可以在 SQL 中执行此操作。诀窍是处理你有 2 + 2 的情况。你必须忽略第二个。

下面通过定义一个@sum变量来处理这个问题。这会酌情增加value一倍。如果该值未达到 3,则忽略该行:

select t.*,
       @sum := if(@prevsum := @sum < 3 and @sum + value > 3, @sum, @sum + value) as thesum
from (select *
      from t
      where value > 0
      order by RAND()
     ) t cross join
     (select @sum := 0, @prevsum := 0) const
having (@sum - value) <= 3 and (@prevsum <> @sum)

这保持了@prevsum它知道在“2 + 2”或“1 + 1 + 2”这样的情况下忽略最后一行。

于 2013-06-19T18:19:17.117 回答
0

你的while($row['Value'] < 3)循环永远不会结束,因为你永远不会改变$row.

我会建议这样的事情:

$first = mysql_fetch_assoc(mysql_query("select * from `".$usertable."` where `Value`>0 order by rand() limit 1"));
$result = array($first);
switch($first['Value']) {
case 1:
    $val = rand(1,2);
    $rows = 3-$val; // 2 if $val is 1, 1 if $val is 2
    $q = mysql_query("select * from `".$usertable."` where `Value`=".$val." order by rand() limit ".$rows);
    while($r = mysql_fetch_assoc($q)) $result[] = $r;
    break;
case 2:
    $result[] = mysql_fetch_assoc(mysql_query("select * from `".$usertable."` where `Value`=1 order by rand() limit 1"));
    break;
// case 3 does nothing special
}
// result rows are in $result[] array.
于 2013-06-19T18:11:47.020 回答
0

您的 mysql_query 在错误的位置。

您应该意识到,该查询是在调用 mysql_query 的地方执行的,因此您在 while 中一遍又一遍地循环同一行...

这就是为什么你得到最大的执行时间......

编辑

$query = "SELECT * FROM $usertable WHERE Value>0 ORDER BY RAND() LIMIT 1"; //from all positive Values, get one at random
$query2 = "SELECT * FROM $usertable WHERE Value BETWEEN 1 AND 2 ORDER BY RAND() LIMIT 1"; //from all Values between 1 and 2, get one at random
$query3 = "SELECT * FROM $usertable WHERE Value=1 ORDER BY RAND() LIMIT 1"; //from all Values equal to 1, get one at random


$result = mysql_query($query);
$row = mysql_fetch_array($result);
echo $row['A'] . " " . $row['B'] . " " . $row['C'] . " " . $row['Value'];
echo "<br>";

if ($row['Value'] < 3)
{
    switch($row['Value'])
    {
        case 2: //If the "Value" is 2, display that row, plus another random row with a "Value" of 1. - thats your query3
            $result3 = mysql_query($query3);
            $row3 = mysql_fetch_array($result3);
            echo $row3['A'] . " " . $row3['B'] . " " . $row3['C'] . " " . $row3['Value'];
            break;

        case 1: //If the "Value" is 1, display that row, plus either one more row with a "Value" of 2, or 2 more rows each with a "Value" of 1. - your query2
            $result2 = mysql_query($query2);
            $row2 = mysql_fetch_array($result2);
            echo $row2['A'] . " " . $row2['B'] . " " . $row2['C'] . " " . $row2['Value'];
            if($row2['Value'] == 1)
            { //select another row with value of 1 - your query3
                $result3 = mysql_query($query3);
                $row3 = mysql_fetch_array($result3);
                echo $row3['A'] . " " . $row3['B'] . " " . $row3['C'] . " " . $row3['Value'];
            }
            break;
    }
}

请注意,此代码可以多次选择一行 - 您没有提到,行应该是唯一的。如果您需要唯一的行,则需要更改 sql 代码以不选择所有已选择的行(即通过某些唯一标识符或返回的完整结果集)

于 2013-06-19T18:16:37.053 回答