在SQL查询中需要很少的修改

时间:2012-05-25 06:23:17

标签: php sql

以下是我的sql查询,其中来自facility_id我要分配来自$ 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';

2 个答案:

答案 0 :(得分:4)

小错字错误在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';

和下一个建议:无需用双引号"

附上$ fid

这里我假设你的字段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';

答案 1 :(得分:1)

请试试这个:

$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";