在 SQL Select 搜索中格式化字符串数据

Formatting String data in an SQL Select Search

老实说,我不确定该如何表达。所以我只是想解释一下这个问题

为了找到尽可能多的匹配项,我目前运行一段代码将数据转换为大写,删除空格和无效字符。例如:

Hi there, I'm very happy! -> HITHEREIMVERYHAPPY

我的数据库已达到这样的大小,运行 一段检查 ResultSet 中每一行的代码不再有效,所以我想知道这在 [=14] 中是否可行=]不知何故?

SELECT * FROM Data WHERE Input.replaceAll("[^a-zA-Z0-9]", "").replace(" ", "").toUpperCase() = INPUT AND Response.replaceAll("[^a-zA-Z0-9]", "").replace(" ", "").toUpperCase() = RESPONSE

只需替换空格...

https://www.w3schools.com/sql/func_sqlserver_replace.asp

SELECT * 从 TABLE WHERE REPLACE(TABLE.COLUMN,' ','') = VALUE;

您可能会喜欢 RLIKE

-- Sample data
create table Data
(
  Id int primary key auto_increment,
  Input varchar(100),
  Response varchar(100)
);
insert into Data (Input, Response) values
  ('Hi there, I''m very happy! ->', 'To have 1 solution.')
, ('HITHEREIMVERYHAPPY', 'ToHave1Solution')
, ('I rlike this', 'IRLikeThis')

查询:

SELECT *
FROM Data
WHERE Input RLIKE '[^ A-Za-z0-9]'
  AND Response RLIKE '[^ A-Za-z0-9]'

结果:

Id | Input                        | Response           
-: | :--------------------------- | :------------------
 1 | Hi there, I'm very happy! -> | To have 1 solution.

db<>fiddle here

它基本上使用正则表达式 [^ A-Za-z0-9],它匹配包含任何非字母、数字或 space.

字符的字符串