我该怎么做这个SQL过滤结果

时间:2018-11-12 12:33:58

标签: php mysql sql phpmyadmin

我的网站有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;
}

1 个答案:

答案 0 :(得分:0)

我认为您应该先尝试理解代码,然后再对其进行修改……但是无论如何:

您只需要将条件附加到PHP代码中生成的where子句中。 您可以通过在此行之后添加标记为“此行是新行”的行来做到这一点:

$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 = preg_replace('/and/','where',$Where,1);

所以这应该起作用-假设$Get['type'] == 'New'意味着这是我只能猜测的新帖子。