gpt4 book ai didi

mysql - 查询未选择的最接近值

转载 作者:行者123 更新时间:2023-11-29 22:19:14 25 4
gpt4 key购买 nike

我认为下面的代码中的这一行: Poscode , ABS( Poscode - :distance ) AS distance 被忽略:

所以它只是考虑这一行,因此返回所提供邮政编码的确切记录,而不是列出与给定邮政编码最接近的所有记录!

$bindings[] = array(":distance", $postcode, PDO::PARAM_STR);

查询:

$rate=$data['slider1'];

$key =array_keys($data['sub']);
//print_r($key);
$pricing2 = $data['slider1'];
$pricing = $pricing2 * 1.15;
$postcode = $data['postcode'];


$bindings = array();
$bindings[] = array(":pricing", $pricing, PDO::PARAM_STR);
$bindings[] = array(":distance", $postcode, PDO::PARAM_STR);

$key_placeholders = array();
foreach($key as $k => $v) {
$placeholder = ":subid".$k;
$bindings[] = array($placeholder, $v, PDO::PARAM_INT);
$key_placeholders[] = $placeholder;
}
$sql = "SELECT Name,PostUUID,pricing,Poscode , ABS( Poscode - :distance ) AS distance,subname,Reputation,ReviewPlus,ReviewNeg,week_morning,week_afternoon,week_evening,weekend_morning,week_afternoon,week_evening,weekend_morning,weekend_afternoon,week_evening,weekend_morning,weekend_afternoon,weekend_evening,date,Phone,Sex,UUID,catname FROM posts,subjects,categories "
. "WHERE posts.subid IN (". implode(",",$key_placeholders).") "
. "AND posts.pricing <=:pricing "
. "AND posts.Poscode =:distance "
. "AND posts.subid=subjects.subid "
. "AND subjects.catid=categories.catid "
. "ORDER BY distance "
. "DESC LIMIT 100";

$statement = $pdo->prepare($sql);
foreach($bindings as $b) {
$statement->bindValue($b[0],$b[1],$b[2]);
}
$statement->execute();

最佳答案

语句的 SELECT 部分选择为每行返回哪些数据 - 因此 ABS( Poscode - :distance ) AS distance 将返回一个名为 的字段distance 即每行的字段 Poscode 与值 :distance 之间的差。

语句的 WHERE 部分选择要返回的行。

因此,如果您想更改返回的行(以获取给定距离内的条目),您需要更改 WHERE 子句。

关于mysql - 查询未选择的最接近值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30911915/

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