LIKE ANY Matches Multiple Patterns

Match a column against several patterns at once with LIKE ANY and ILIKE ANY, including patterns supplied by a subquery.

SqlData Fetching

Code

snippet
-- LIKE ANY:一次匹配数组中的多个模式
SELECT id, name
FROM products
WHERE name LIKE ANY (ARRAY['%pro%', '%max%', '%ultra%'])
ORDER BY name;

-- 大小写不敏感版本
SELECT id, name
FROM products
WHERE name ILIKE ANY (ARRAY['%pro%', '%max%']);

-- 模式来自另一张表:用子查询构造数组
SELECT id, name
FROM products
WHERE name LIKE ANY (SELECT pattern FROM search_patterns)
ORDER BY id;

-- 对照:多个 OR 条件等价但更冗长
SELECT id, name
FROM products
WHERE name LIKE '%pro%' OR name LIKE '%max%';

-- NOT LIKE ALL 是相反判断:排除所有模式
SELECT id
FROM products
WHERE name NOT LIKE ALL (ARRAY['%test%', '%demo%']);

What the code does

Several statements share this file — SELECT, with products and search_patterns among the objects involved.

Mechanically, the statements are built from: a WHERE clause narrowing which rows one statement affects; an ORDER BY that fixes the output order.

Applying it

Run the statement against the products table.

Statements that matter

SELECT id, name
FROM products
WHERE name LIKE ANY (SELECT pattern FROM search_patterns)
ORDER BY id;
SELECT id, name
FROM products
WHERE name LIKE ANY (ARRAY['%pro%', '%max%', '%ultra%'])
ORDER BY name;
SELECT id, name
FROM products
WHERE name ILIKE ANY (ARRAY['%pro%', '%max%']);

Interactive Tools for This Topic

FAQ

Which tables appear in this script?
`products`, `search_patterns` — referenced directly, no join needed.