【问题标题】:How can I do this sql filter results我该怎么做这个sql过滤结果
【发布时间】:2018-11-12 12:33:57
【问题描述】:

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

【问题讨论】:

  • ..我不知道如何在 php 代码中做到这一点...... 为什么在两者中都这样做?你说 mysql 正在工作……为什么是 php?
  • @B001ᛦ 他想用 PHP 来做。他使用 MySQL 所做的事情是一样的。他想知道在他的 PHP 代码中哪里可以使用相同的查询并替换旧的查询。
  • 是的...我想用 PHP 来做。他使用 MySQL 所做的事情是一样的。我想知道他在哪里可以在他的 PHP 代码中使用相同的查询并替换旧的查询

标签: php mysql sql phpmyadmin


【解决方案1】:

我认为你应该在修改代码之前先尝试理解代码......但无论如何:

您只需要将条件附加到 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 .......

正如你所见,任何多余的首字母“and”都被替换为“where”:

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

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

【讨论】:

  • 感谢您的回复..您的编辑后,它不起作用,它只是显示空白页。
  • 您检查过语法或 SQL 错误吗? wid 的数据类型可能是整数,但我不知道。因此,您可能想再次更改代码。我已经更新了上面的新行。您可能还想在进行更改后发布整个代码。我也可以看看。
  • 我测试了添加这些作品:and S.wid = 1 or S.wid = 2 or S.wid = 3
  • 您必须非常小心,因为 WHERE 子句会生成多个 AND。如果你没有使用括号,比如 in: and (S.wid = 1 or S.wid = 2 or S.wid = 3 or S.wid = 4) 你可能会遇到麻烦。
  • 改成这些,效果很好>> $Where .= ' and S.wid = 1 or S.wid = 2 or S.wid = 3 or S.wid = 4'; //这条线是新的
猜你喜欢
  • 2014-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-23
  • 1970-01-01
  • 2022-11-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多