gpt4 book ai didi

MySQL Multi Query - 构建额外的 SQL 参数

转载 作者:行者123 更新时间:2023-11-30 01:06:04 31 4
gpt4 key购买 nike

我有以下代码:

$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 查询中。

例如,用户可能从下拉列表中选择一个选项,然后需要将其 append 到 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';";

可能有一个 append (如上所述),也可能有多个 append 。

我以前从未尝试过这样的事情,所以我想知道在我深入研究之前是否有人有任何建议?

最佳答案

这是我的一般方法:

$filter = "AND q16 = 'Yes'"; //This value is dynamic depending on dropdown selection
$query .= "SELECT COUNT( ". $r ." ) AS Total,
(SELECT COUNT( ". $r ." ) FROM tresults WHERE ". $r ." >=750 " . $filter ." ) AS Eng,
(SELECT COUNT( ". $r ." ) FROM tresults WHERE ". $r ." >=450 AND ". $r ." <=749 " . $filter ." ) AS Pass,
(SELECT COUNT( ". $r ." ) FROM tresults WHERE ". $r ." <=449 " . $filter ." ) AS Dis
FROM tresults WHERE 1=1 " . $filter ." ;";

关于MySQL Multi Query - 构建额外的 SQL 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19730797/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com