我有以下代码:
$query .= "SELECT COUNT( ". $r ." ) AS Total,
(SELECT COUNT( ". $r ." ) FROM tresults WHERE ". $r ." >=750) AS Eng,
(SELECT COUNT( ". $r ." ) FROM tresults WHERE ". $r ." >=450 AND ". $r ." <=749) AS Pas,
(SELECT COUNT( ". $r ." ) FROM tresults WHERE ". $r ." <=449) AS Dis
FROM tresults;";
这是一个多重查询,使用 PHP 我可以查看多个实例并拉回数据——这很好。
但是,我需要做的是根据用户从下拉菜单中输入的内容轻松地向 SQL 查询添加其他参数。
因此,例如,用户可能会从下拉列表中选择一个选项,然后需要将其附加到 SQL 查询中,例如:
$query .= "SELECT COUNT( ". $r ." ) AS Total,
(SELECT COUNT( ". $r ." ) FROM tresults WHERE ". $r ." >=750 AND q16 = 'Yes') AS Eng,
(SELECT COUNT( ". $r ." ) FROM tresults WHERE ". $r ." >=450 AND ". $r ." <=749 AND q16 = 'Yes') AS Pass,
(SELECT COUNT( ". $r ." ) FROM tresults WHERE ". $r ." <=449 AND q16 = 'Yes') AS Dis
FROM tresults WHERE q16 = 'Yes';";
可能有一个附加,如上所述,或多个。
我以前没有尝试过这样的事情,所以我想知道在我深入研究之前是否有人有任何建议?