我如何做这个sql过滤结果

r3i60tvu  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(242)

我有一个网站有5个类别,我只想显示在新的职位只有1-4类..我如何编辑代码来做这项工作?谢谢
这是我的代码,我在sql中使用以下代码:

SELECT * FROM `ff_se` WHERE Wid in ('1','2','3','4')"

但我不知道如何在php代码中做到这一点。以下是我的php代码部分:

$this->TableSe = 'ff_se';
    $this->TableSeWord = 'ff_se_word';
    $this->TableSeSupport = 'ff_se_support';
    $this->TableSePost = 'ff_se_post';
    $this->TableSePostTableId = 'ff_se_post_tableid';

public function GetAjaxList($Get){
    $Results = array();
    $Get = $this->StrToGBK($Get);
    $Page = $Get['page'] ? intval($Get['page']):0;

    $Where = '';
    $Order = 'S.updateline';
    if($Get['type'] == 'New'){
        $Order = 'S.dateline';
    }else if($Get['type'] == 'Hot'){
        $Order = 'S.updateline';
        if($this->Config['PluginVar']['ListHotVal']){
            $Where .= ' and (S.support_count >= '.$this->Config['PluginVar']['ListHotVal'].' OR S.comment_count >= '.$this->Config['PluginVar']['ListHotVal'].')';
        }
        if($this->Config['PluginVar']['ListHotDataline']){
            $Where .= ' and S.dateline >= '.strtotime("-".$this->Config['PluginVar']['ListHotDataline']." hours",time());   
        }
    }else if($Get['type'] == 'Nearby'){//
        $Order = 'S.updateline';
        if($Get['lng'] && $Get['lat']){
            $SquarePoint = $this->GetReturnSquarePoint($Get['lng'],$Get['lat'],$this->Config['PluginVar']['Distance']);
            $Where .= ' and S.lat <> 0 and S.lat > '.$SquarePoint['right-bottom']['lat'].' and S.lat < '.$SquarePoint['left-top']['lat'].' and S.lng > '.$SquarePoint['left-top']['lng'].' and S.lng < '.$SquarePoint['right-bottom']['lng'];
        }else{
            return $Results;
        }
    }

    if($_GET['wid']){
        $Where .= ' and S.wid = '.intval($_GET['wid']);
    }
    $Where .= ' and S.display = 1 and S.fast_add_display = 1';
    $Where = preg_replace('/and/','where',$Where,1);

    $this->Config['PluginVar']['ListNum'] = $this->Config['PluginVar']['ListNum'] ? $this->Config['PluginVar']['ListNum'] : 10;
    $Limit = 'LIMIT '.($Page * $this->Config['PluginVar']['ListNum']).','.$this->Config['PluginVar']['ListNum'];

    $FetchSql = 'SELECT W.title as Ttitle,S.* FROM '.DB::table($this->Tablese).' S LEFT JOIN '.DB::table($this->TableSeWord).' W on W.id = S.wid '.$Where .' order by topdateline > '.time().' desc,'.$Order.' desc,S.dateline desc '.$Limit;
    $Results = $this->ListFormat(DB::fetch_all($FetchSql));
    return $Results;
}
qkf9rpyu

qkf9rpyu1#

我认为你应该在修改代码之前先尝试理解它。。。但不管怎样:
您只需要将条件附加到php代码中生成的where子句。您可以通过在此行后添加标记为“this line is new”的行来完成此操作:

$Where = '';

这是代码的相关部分:

$Where = '';
$Order = 'S.updateline';
if($Get['type'] == 'New'){
    $Where .= ' and S.wid in (1,2,3,4)'; //THIS LINE IS NEW
    $Order = 'S.dateline';
}else .......

如您所见,任何多余的首字母“和”在此处替换为“where”:

$Where = preg_replace('/and/','where',$Where,1);

所以这应该管用-假设 $Get['type'] == 'New' 意味着这是一个新的职位,我只能猜测。

相关问题