在sql查询中很少修改需要

以下是我的SQL查询中facility_id我wat分配值来自$ fid。请让我知道下面的代码有什么问题,以便我可以相应地删除它们

$fid = 101;
$q = 'select r_present,r_reminder 
    from z_events 
    where resident_id = ? 
        and r_added > date_sub(curdate(), interval 37 day) 
        and fascility_id='."$fid". '
    group by r_added 
    order by r_added 
    desc limit 4';
1
额外 编辑
意见: 1

2 答案

小错字错误在fid和group之间增加空格如下:

$q = 'select r_present,r_reminder 
from z_events 
where resident_id = ? 
    and r_added > date_sub(curdate(), interval 37 day) 
    and fascility_id='.$fid. ' 
group by r_added 
order by r_added 
desc limit 4';

and next suggestion: no need to enclose $fid with double quote "

在这里,我假设你的领域facility_id是整数类型。如果它的字符串,然后添加双引号“”它像:

$q = 'select r_present,r_reminder 
from z_events 
where resident_id = ? 
    and r_added > date_sub(curdate(), interval 37 day) 
    and fascility_id="'.$fid. '" 
group by r_added 
order by r_added 
desc limit 4';
4
额外

请试试这个:

$fid = 101;
    $q = "select r_present,r_reminder from z_events where resident_id = ? and r_added > date_sub(curdate(), interval 37 day) and fascility_id=$fid group by r_added order by r_added desc limit 4";
1
额外
当不使用字符串连接时,使用 {$ fid} 是很好的。 :)
额外 作者 hjpotter92,