How does the LIKE operator work, and when should you use full-text search instead?
How does the LIKE operator work, and when should you use full-text search instead?
LIKE matches a string against a pattern. % matches any number of characters (including none) and _ matches exactly one character.
SELECT * FROM users WHERE name LIKE 'Ann%'; -- starts with Ann
SELECT * FROM users WHERE name LIKE '%son'; -- ends with son
SELECT * FROM users WHERE code LIKE 'A_9%'; -- A, any char, 9, anything
SELECT * FROM files WHERE name LIKE '100\%%' ESCAPE '\'; -- literal percent sign
It depends on the engine and collation: LIKE is case-sensitive in PostgreSQL and Oracle by default (PostgreSQL offers ILIKE), but usually case-insensitive in MySQL and SQL Server because of their default collations.
'abc%') can use a B-tree index. In PostgreSQL with a non-C collation, create the index with text_pattern_ops for this to work.Đăng nhập để tham gia thảo luận cùng mọi người.
Chưa có bình luận nào
Hãy là người đầu tiên chia sẻ suy nghĩ của bạn.