首页 > 数据库 > 数据库开发技术 > 五种主流数据库:字符串匹配
| 字符串模糊匹配 | MySQL | Oracle | SQL Server | PostgreSQL | SQLite |
|---|---|---|---|---|---|
| [NOT] LIKE | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
| [NOT] ILIKE | ❌ | ❌ | ❌ | ✔️ | ❌ |
| REGEXP_LIKE() | ✔️ | ✔️ | ❌ | ✔️ | ❌ |
| [NOT] SIMILAR TO | ❌ | ❌ | ❌ | ✔️ | ❌ |
| 全文检索 | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
SELECT emp_name
FROM employee
WHERE emp_name LIKE '关%';
emp_name
--------
关兴
关平
关羽
百分号(%),表示匹配零个或者多个任意字符。
下画线(_),表示匹配一个任意字符。
LIKE ‘en%’,匹配以“en”开始的字符串,例如“english”、“end”。
LIKE ‘%en%’,匹配包含“en”的字符串,例如“length”、“when”。
LIKE ‘%en’,匹配以“en”结束的字符串,例如“ten”、“when”。
LIKE ‘Be_’,匹配以“Be”开头,再加上一个任意字符的字符串。例如“Bed”、“Bet”。
LIKE ‘_e%’,匹配一个任意字符加上“e”开始的字符串,例如“he”、“year”。
CREATE TABLE t_like(c1 VARCHAR(200));
INSERT INTO t_like(c1) VALUES ('项目进度:25%已完成');
INSERT INTO t_like(c1) VALUES ('记录日期:2021 年 5 月 25 日');
SELECT c1
FROM t_like
WHERE c1 LIKE '%25#%%' ESCAPE '#';
c1
----------
项目进度:25%已完成
SELECT email
FROM employee
WHERE email LIKE 'M%';
-- MySQL、Microsoft SQL Server 以及 SQLite
email
----------------
madai@shuguo.com
mizhu@shuguo.com
-- Oracle 以及 PostgreSQL
email
-----
SELECT c1
FROM t_like
WHERE c1 NOT LIKE '%25#%%' ESCAPE '#';
c1
---------------
记录日期:2021 年 5 月 25 日
SELECT regexp_like('SQL', '^s', 'c'), regexp_like('SQL', '^s', 'i');
regexp_like('SQL', '^s', 'c')|regexp_like('SQL', '^s', 'i')|
-----------------------------+-----------------------------+
0| 1|
-- PostgreSQL
SELECT 'SQL' SIMILAR TO '[A-Z]{3}';
?column?|
--------+
true |
https://dev.mysql.com/doc/refman/8.0/en/fulltext-search.html
https://docs.oracle.com/en/database/oracle/oracle-database/23/ccapp/understanding-oracle-text-application-development.html#GUID-CF13C01A-F5E6-4EF5-839B-C09CF0024D5E
https://learn.microsoft.com/en-us/sql/relational-databases/search/full-text-search?view=sql-server-ver16
https://www.postgresql.org/docs/current/textsearch-intro.html
https://www.sqlite.org/fts5.html
原文作者:SQL编程思想;如有侵权,请联系管理员删除。