如您所知,如果您使用.. where col_name like "%word%",那么它將作為通配符進行搜索。好的,一切都是檔案。現在我想要反之亦然。我的意思是,我想根據條目制作列通配符。
請看看這個:
// tb_name
--------------
| col_name |
--------------
| Ali |
| Martin |
| John |
--------------
我想通過這個值匹配第三行:John Foo。或通過此條目匹配第一行:Mr Ali。所以conceptically我想是這樣的:.. where %col_name% like "word"。我怎樣才能在 MySQL 中做到這一點?
uj5u.com熱心網友回復:
您將通配符%連接到 col_name。
然后你就可以喜歡 John Foo。
select *
from tb_name
where 'John Foo' like concat('%',col_name,'%')
但是如果 col_name 被索引,那么使用IN會更快。
因為concat('%',col_name,'%')不是sargable。
select *
from tb_name
where col_name IN ('John','Foo')
或者更復雜的方法,通過從名稱字串中獲取部分。
select t.*
from tb_name t
cross join (select 'John Foo Bar' name) names
where t.col_name IN (
substring_index(name,' ',1),
substring_index(substring_index(name,' ', 2),' ',-1),
substring_index(substring_index(name,' ', 3),' ',-1)
)
關于db<>fiddle 的演示在這里
uj5u.com熱心網友回復:
您也許可以使用LOCATE()。
SELECT * FROM tb_name WHERE LOCATE(name, 'John Foo') > 0
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/386891.html
