1

我试图在一个垃圾箱中随机选择 30% 的票。下面的代码适用于 30%,但是当我在末尾添加“ORDER BY RND(INT(NOW*ID)-NOW*ID)”时,出现错误。

作品:

$result1 = "SELECT MainBin.Assignee, Count(MainBin.IncidentNumber) * .3 AS CountOfIncidentNumber FROM  MainBin GROUP BY MainBin.Assignee";
$rs1=odbc_exec($conn,$result1);
while($row = odbc_fetch_array($rs1))
{
$Assignee = "". $row['Assignee'];
$CountOfIncidentNumber = ceil($row["CountOfIncidentNumber"]);
$result2 = "INSERT INTO TicketBin (IncidentNumber)
SELECT TOP $CountOfIncidentNumber IncidentNumber FROM MainBin 
WHERE Assignee = '$Assignee'";
$rs2=odbc_exec($conn,$result2);
}

不工作:

 $result1 = "SELECT MainBin.Assignee, Count(MainBin.IncidentNumber) * .3 AS CountOfIncidentNumber FROM  MainBin GROUP BY MainBin.Assignee ORDER BY RND(INT(NOW*ID)-NOW*ID)";
 $rs1=odbc_exec($conn,$result1);

我有一个名为 ID 的 AutoNumber 字段,我以前用过这个,只是没有分组。任何帮助表示赞赏!

4

1 回答 1

2

假设当您“遇到错误”时,您的意思是错误

您尝试执行的查询不包含指定表达式 'RND(INT(NOW()*ID)-NOW()*ID)' 作为聚合函数的一部分。

那么你可以尝试使用这样的方法

SELECT
        Assignee,
        CountOfIncidentNumber
FROM
        (
            SELECT 
                    MainBin.Assignee, 
                    COUNT(MainBin.IncidentNumber) * .3 AS CountOfIncidentNumber,
                    MIN(ID) AS MinOfID
            FROM MainBin 
            GROUP BY MainBin.Assignee
        )
ORDER BY RND(INT(NOW*MinOfID)-NOW*MinOfID)

编辑回复:评论

听起来您可能需要保留原始代码并改用它:

$result2 = "INSERT INTO TicketBin (IncidentNumber)
SELECT TOP $CountOfIncidentNumber IncidentNumber FROM MainBin 
WHERE Assignee = '$Assignee' ORDER BY RND(INT(NOW*ID)-NOW*ID)";
于 2013-08-23T15:06:54.237 回答