首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >sql查询where子句在值中添加额外的空间

sql查询where子句在值中添加额外的空间
EN

Stack Overflow用户
提问于 2016-07-19 10:28:42
回答 2查看 777关注 0票数 0

我在DB中搜索一个关键字,它是Bootjack Fire and Rescue Foundation,因为单词后面只有一个空格,我的查询是

代码语言:javascript
复制
WHERE `title` LIKE  '%Bootjack Fire and  Rescue Foundation%'

为什么搜索标题中的and关键字后面有两个空格,就像您在查询中看到的那样?

添加查询

代码语言:javascript
复制
$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');
EN

回答 2

Stack Overflow用户

发布于 2016-07-19 10:38:48

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

代码语言:javascript
复制
$where="replace(replace(replace(store_title,' ','<>'),'><',''),'<>',' ') LIKE  '% replace(replace(replace(".$keyword".,' ','<>'),'><',''),'<>',' ') %'";

考虑到<>符号不会出现在该列值中,否则需要使用其他符号。我已经在SQL-SERVER中使用了-SERVER也应该在Mysql中工作.

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

代码语言:javascript
复制
$keyword=preg_replace('/\s+/', ' ',$keyword);

然后使用简单的sql作为..。

代码语言:javascript
复制
$where="store_title LIKE '% ".$keyword" %'";
票数 1
EN

Stack Overflow用户

发布于 2016-07-19 10:44:00

我认为在查询中是不可能的。

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

  • 分拆短语-- http://php.net/manual/en/function.str-split.php
  • 修整所有单词以删除空格。-- http://php.net/manual/en/function.trim.php
  • 用一个空格正确地重新创建短语。

希望它能帮到你。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38455939

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档