簡體   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