我想要运行类似这样的代码:
select * from table where field in ("%apple%", "%orange%")
有没有一种方法?或者至少比为每个关键字动态构建查询更好的方法:
select * from table where field like "%apple%" or field like "%orange%"
谢谢。
我不确定它是否比你想出来的更好,但你可以使用MySQL的正则表达式功能:
select * from my_table where field rlike 'apple|orange';
此外,正如其他人所提到的,您可以使用 MySQL 的全文搜索 功能(但仅适用于使用 MyISAM 引擎的情况)。
编辑:我查了一下,它只支持在单词的末尾使用通配符:
ALTER TABLE table ADD FULLTEXT INDEX (field);
SELECT * FROM table WHERE MATCH (field)
AGAINST ('orange* apple*' IN BOOLEAN MODE);
select * from table where
regexp_like (column, 'apple|orange', 'i')
您可以使用更复杂的正则表达式。'i'使其不区分大小写。 请参考Oracle文档