【问题标题】:MYSQL LIKE in same column several timesMYSQL LIKE 在同一列中多次
【发布时间】:2013-03-13 14:03:49
【问题描述】:

我有 3 个下拉列表的过滤器,每个列表都包含相同的条件(运营商、服务、SIM 卡)。当这些下拉列表包含不同的搜索类别时,搜索功能可以正常工作,但如果 2 或 3 个下拉列表中的类别相同,则过滤器无法正常工作。由于 SQL 查询不正确:

"SELECT * FROM table WHERE id = " . $_GET['id'] . " AND operator like %tele% AND operator like %bite%"

应该是

"SELECT * FROM table WHERE id = " . $_GET['id'] . " AND operator like %tele% OR operator like %bite%"

这是我的过滤器 php 代码:

 $this->_sql['where'].= $this->reg['post']['flt_find_1'] ? " AND ".$this->reg['post']['flt_field_1']." like '%".$this->reg['post']['flt_find_1']."%' " : "";      
 $this->_sql['where'].= $this->reg['post']['flt_find_2'] ? " AND ".$this->reg['post']['flt_field_2']." like '%".$this->reg['post']['flt_find_2']."%' " : "";  
 $this->_sql['where'].= $this->reg['post']['flt_find_3'] ? " AND ".$this->reg['post']['flt_field_3']." like '%".$this->reg['post']['flt_find_3']."%' " : "";  

如何检查过滤器中的所有类别是否不同,然后为每个类别使用AND,但类别相同,则使用OR(或者可能有其他解决方法)。

谢谢。

【问题讨论】:

  • "SELECT * FROM table WHERE id = " . $_GET . " AND operator like %tele% OR operator like %bite%" - 你为什么要连接一个字符串和一个数组?
  • @Sann 我弄错了,我已经编辑了我的问题。
  • 你没有消毒。停在那里,重新思考你在做什么。
  • @Hiroto 我知道,我只是为这个问题写的,这不是我要的,谢谢。
  • 请也出示您的html代码。

标签: php mysql


【解决方案1】:
  1. 您的代码容易受到 sql 注入的影响,因此请检查 get 参数;
  2. 如果我理解正确,请尝试如下:
SELECT * FROM table WHERE id = '".($_GET['id']*1)."' AND ( operator LIKE '%tele%' AND operator LIKE '%bite%' )";

【讨论】:

  • 是的那部分我知道并且它正在工作,问题是我不知道如何在 PHP 部分中检查它。
  • 你能解释的更具体一点,你不明白什么?因为查询已经返回了所有需要的数据。
  • 在我的原始帖子中是 PHP 代码的一部分,我需要检查何时使用 OR 以及何时使用 AND。因为现在如您所见,我的过滤器总是对每个过滤器类别使用 AND。对不起我的英语。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-27
  • 2017-07-28
  • 2016-06-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多