简体   繁体   中英

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?". I'm trying to get the value "He?llo?" where using the equal to "Hello" to get it. It doesn't equal because "He?llo?" has 2 question marks inside of it, and "Hello" has none. My question is, is there a way to somehow remove the 2 question marks from "He?llo?" before comparing it to "Hello", so that it equals?

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

You can use replace() :

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

You could use replace as many times as you like. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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