简体   繁体   English

使用Equals从数据库中获取价值,而忽略某些字母

[英]Getting value from database using equals while ignoring certain letters

I have a column in MYSQL named 'text', and it has one value: "He?llo?". 我在MYSQL中有一列名为“文本”,它具有一个值:“ He?llo?”。 I'm trying to get the value "He?llo?" 我正在尝试获取值“ He?llo?” where using the equal to "Hello" to get it. 使用等于“ Hello”的方式获取它。 It doesn't equal because "He?llo?" 这并不相等,因为“嘿?” has 2 question marks inside of it, and "Hello" has none. 其中有2个问号,而“ Hello”则没有。 My question is, is there a way to somehow remove the 2 question marks from "He?llo?" 我的问题是,是否有办法删除“ He?llo”中的2个问号? before comparing it to "Hello", so that it equals? 在将其与“ Hello”进行比较之前,等于吗?

$text = "Hello";
SELECT text FROM table WHERE text = '$text';

You can use replace() : 您可以使用replace()

SELECT text
FROM table
WHERE replace(text, '?', '') = '$text';

You could use replace as many times as you like. 您可以根据需要多次使用replace But this way of proceeding will require a full table scan, and if your table has many rows this is inefficient. 但是,这种处理方式将需要对整个表进行扫描,并且如果表中有很多行,则效率很低。

There are two alternatives : 有两种选择:

  • look into fulltext index. 查看全文索引。 It could fit your needs, and if it does, it is the most efficient way to solve your problem. 它可以满足您的需求,如果可以,这是解决问题的最有效方法。
  • add a second column and fill it with the cleaned up text. 添加第二列,并用清理后的文本填充它。 Then you can compare your text in an efficient way,using the indexes. 然后,您可以使用索引高效地比较文本。

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

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