当模式位于字段中时如何编写查询

发布于 2025-01-05 16:33:34 字数 276 浏览 1 评论 0原文

我有一个表,其中字段包含“hey *”或“%test%”等模式。 像这样:

Id - f_pattern - f_Respond
1  - 'hey *' - 'hello there'
2  - 'how are you *' - 'am fine'

我是否可以像这样编写查询:

select * from table where f_pattern like 'hey bobby'

它返回第一行?

I've got a table in which a field contains pattern like 'hey *' or '%test%'.
Like this :

Id - f_pattern - f_Respond
1  - 'hey *' - 'hello there'
2  - 'how are you *' - 'am fine'

Is is possible that i write query like this :

select * from table where f_pattern like 'hey bobby'

and it returns the first row ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ぶ宁プ宁ぶ 2025-01-12 16:33:34

是的,如果您将模式更改为“类似兼容”值,并反转类似逻辑:

select * from pattern_table
where 'hey bobby' like replace(f_pattern, '*', '%')

Yes, if you change your patterns to "like compatible" values, and reverse the like logic:

select * from pattern_table
where 'hey bobby' like replace(f_pattern, '*', '%')
南城追梦 2025-01-12 16:33:34

你试过

select * from `table` where field like 'hey %'

//编辑

它让我惊讶,但这有效:

create database test;    
use test;
create table pattern (a varchar (100));
insert into pattern values ('a%');
create table subject (a varchar (100));    
insert into subject values ('abc'), ('cde');
select * from subject where a like (select a from pattern limit 1);

-- result
-- +------+
-- | a    |
-- +------+
-- | abc  |
-- +------+
-- 1 row in set (0.03 sec)

请参阅: http://dev.mysql.com/doc/refman/5.0/en/pattern-matching.html

have you tried

select * from `table` where field like 'hey %'

//edit

it surprised me, but this works:

create database test;    
use test;
create table pattern (a varchar (100));
insert into pattern values ('a%');
create table subject (a varchar (100));    
insert into subject values ('abc'), ('cde');
select * from subject where a like (select a from pattern limit 1);

-- result
-- +------+
-- | a    |
-- +------+
-- | abc  |
-- +------+
-- 1 row in set (0.03 sec)

see: http://dev.mysql.com/doc/refman/5.0/en/pattern-matching.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文