繁体   English   中英

sql查询,其中like子句在值中添加额外的空间

[英]sql query where like clause adding extra space in value

我正在搜索Bootjack Fire and Rescue Foundation数据库中的一个关键字,因为一个单词后有一个空格,所以我的查询是

WHERE `title` LIKE  '%Bootjack Fire and  Rescue Foundation%'

在查询中,为什么在搜索标题中的and关键字之后有两个空格?

添加查询

$keyword = 'Bootjack Fire and Rescue Foundation';

$this->db->select('stores.id,store_title,store_category_id,store_sub_category_id,store_sub_category_type_id,sub_category_fourth,store_link');
$this->db->from('stores');
$this->db->join('users','users.id=stores.user_id');
$this->db->join('store_category','stores.store_category_id=store_category.id');

$where="`store_title` LIKE  '%".$keyword."%'";
$this->db->where($where);
$this->db->where('store_status', 1);
$this->db->where('store_type', 2);
$this->db->where('store_category.status', 1);
$this->db->order_by('store_title','ASC');
$this->db->order_by('store_category_id');

您可以将多个空间转换为一个空间,然后使用相同的like子句。

$where="replace(replace(replace(store_title,' ','<>'),'><',''),'<>',' ') LIKE  '% replace(replace(replace(".$keyword".,' ','<>'),'><',''),'<>',' ') %'";

考虑到<>符号将不会出现在该列值中,否则需要使用其他一些符号。 我在SQL-SERVER中使用过此功能,在Mysql中也应使用。

您可以在php删除多个空格,如下所示...

$keyword=preg_replace('/\s+/', ' ',$keyword);

然后使用简单的SQL作为...

$where="store_title LIKE '% ".$keyword" %'";

我认为不可能直接在查询中。

您可以转换关键字以修复任何错误的数据。

希望对您有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM