简体   繁体   English

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

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

I am searching a keyword in DB that is Bootjack Fire and Rescue Foundation As there is single space after a word, my query is ` 我正在搜索Bootjack Fire and Rescue Foundation数据库中的一个关键字,因为一个单词后有一个空格,所以我的查询是

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

Why are there two space after the and keyword in the search title as you can see in the query? 在查询中,为什么在搜索标题中的and关键字之后有两个空格?

add query 添加查询

$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');

You can convert multiple space into a single one then use the same like clause. 您可以将多个空间转换为一个空间,然后使用相同的like子句。

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

considering < and > symbol will not appear in that column value, otherwise need to use some other symbols. 考虑到<>符号将不会出现在该列值中,否则需要使用其他一些符号。 I have used this in SQL-SERVER should works in Mysql also. 我在SQL-SERVER中使用过此功能,在Mysql中也应使用。

You can remove multiple spaces in php like below... 您可以在php删除多个空格,如下所示...

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

then use the simple sql as... 然后使用简单的SQL作为...

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

I think is not possible directly in the query. 我认为不可能直接在查询中。

You can transform your keyword to repair any wrong data. 您可以转换关键字以修复任何错误的数据。

I hope it helps you. 希望对您有帮助。

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

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