MySQL - 列值中的特殊字符
MySQL - Special characters in column value
我得到了一个大数据(约60万)
我希望显示值为 "word's" 的行。
特殊字符将被完全忽略。
TABLE:
| column_value |
| ------------- |
| word's |
| hello |
| world |
查询:select * from table where column_value like '%words%'
结果:
| column_value |
| ------------- |
| word's |
我希望出现带有特殊字符的行并忽略它们的特殊字符。
你能帮我看看我们怎样才能用快速的运行时间来实现它吗?
您必须在 column_value 上添加索引。
MySQL doc
您可以使用 replace 删除匹配前的 "special" 字符。
SELECT *
FROM table
WHERE replace(column_value, '''', '') LIKE '%words%';
嵌套其他字符的 replace()
调用。
或者你用正则表达式试试
SELECT *
FROM table
WHERE column_value REGEXP 'w[^a-zA-Z]*o[^a-zA-Z]*r[^a-zA-Z]*d[^a-zA-Z]*s';
[^a-zA-Z]*
匹配可选字符,它们不是 a、...、y 和 z,也不是 A、...、Y 和 Z,因此这也匹配您的搜索词以及介于两者之间的任何非字母字母。
或者你看看 full text search 带来的选项。也许这也能有所帮助。
我得到了一个大数据(约60万)
我希望显示值为 "word's" 的行。
特殊字符将被完全忽略。
TABLE:
| column_value |
| ------------- |
| word's |
| hello |
| world |
查询:select * from table where column_value like '%words%'
结果:
| column_value |
| ------------- |
| word's |
我希望出现带有特殊字符的行并忽略它们的特殊字符。
你能帮我看看我们怎样才能用快速的运行时间来实现它吗?
您必须在 column_value 上添加索引。 MySQL doc
您可以使用 replace 删除匹配前的 "special" 字符。
SELECT *
FROM table
WHERE replace(column_value, '''', '') LIKE '%words%';
嵌套其他字符的 replace()
调用。
或者你用正则表达式试试
SELECT *
FROM table
WHERE column_value REGEXP 'w[^a-zA-Z]*o[^a-zA-Z]*r[^a-zA-Z]*d[^a-zA-Z]*s';
[^a-zA-Z]*
匹配可选字符,它们不是 a、...、y 和 z,也不是 A、...、Y 和 Z,因此这也匹配您的搜索词以及介于两者之间的任何非字母字母。
或者你看看 full text search 带来的选项。也许这也能有所帮助。