是否有任何函数可以在 sql 之类的中查找具有特殊字符的结果?
Is there any function for finding the result with special character in a sql like?
我尝试从 SQL 获取数据 LIKE
成功获取数据但结果超出我的需要
这是我的一些数据
apple1
apple2
apple3
applejuice1
applej1
applej2
我的查询:
select * from apple where name like '%applej%'
目前我得到:
applejuice1
applej1
applej2
我的预期输出:
applej1
applej2
您可以使用正则表达式,这将包括包含 'applej' + 0 或 1 个以上字符的所有行
SELECT * FROM test WHERE col1 REGEXP '^applej.?$'
这将查找包含 'applej' + 恰好多 1 个字符的所有行
SELECT * FROM test WHERE col1 REGEXP '^applej.{1}$'
如果'applej'后面的数字可能包含几个数字
SELECT * FROM test WHERE col1 REGEXP '^applej[0-9]+$'
当然,也许您只需要@forpas 在上面的评论中建议的 LIKE
使用 REGEXP
这是可能的:
select *
from apple
where `name` REGEXP '^applej[0-9]'
更新:
如果 name
的数据为 applej1, applej2, applej15, applej109
,则以下查询将起作用:
select *
from apple
where `name` REGEXP '^applej[0-9]+'
你可以试试这个,
SELECT * FROM apple where name LIKE '%applej_'
'_'用于表示SQL服务器中的单个字符。
对于 MS Access,您可以试试这个
SELECT * FROM apple where name LIKE '*applej?'
我找到了
的解决方案
SELECT * FROM apple WHERE MATCH(name) AGAINST('applej')
我尝试从 SQL 获取数据 LIKE
成功获取数据但结果超出我的需要
这是我的一些数据
apple1
apple2
apple3
applejuice1
applej1
applej2
我的查询:
select * from apple where name like '%applej%'
目前我得到:
applejuice1
applej1
applej2
我的预期输出:
applej1
applej2
您可以使用正则表达式,这将包括包含 'applej' + 0 或 1 个以上字符的所有行
SELECT * FROM test WHERE col1 REGEXP '^applej.?$'
这将查找包含 'applej' + 恰好多 1 个字符的所有行
SELECT * FROM test WHERE col1 REGEXP '^applej.{1}$'
如果'applej'后面的数字可能包含几个数字
SELECT * FROM test WHERE col1 REGEXP '^applej[0-9]+$'
当然,也许您只需要@forpas 在上面的评论中建议的 LIKE
使用 REGEXP
这是可能的:
select *
from apple
where `name` REGEXP '^applej[0-9]'
更新:
如果 name
的数据为 applej1, applej2, applej15, applej109
,则以下查询将起作用:
select *
from apple
where `name` REGEXP '^applej[0-9]+'
你可以试试这个,
SELECT * FROM apple where name LIKE '%applej_'
'_'用于表示SQL服务器中的单个字符。 对于 MS Access,您可以试试这个
SELECT * FROM apple where name LIKE '*applej?'
我找到了
的解决方案SELECT * FROM apple WHERE MATCH(name) AGAINST('applej')