SQL: Select 和 "Like" 2 个元素
SQL: Select with "Like" 2 elements
我如何 select 任何名字中有两个字母“a”的人,在 PostgreSQL 数据库中?
我尝试 select client from xclient where client like '%a%'
但这个 return 所有名称中包含“a”的人。我一定要用“赞”。
结果中是否应包括名字中带有两个“a”的人
示例:“玛尔塔”。
例如,不能包含“Alaia”。
试试这个:
select client from xclient where client like '%a%a%'
我几乎可以肯定此代码在您所说的示例中有效:
select client from xclient where client like '%a%a%' and client not like '%a%a%a%' and client not like '%a%aa%' and client not like '%aa%a%' and client not like '%aa%aa%'
毫无疑问,您可以使用 the substring keyword 优化此代码,但是,无论如何,这应该会如您所愿地执行。
你的问题和评论很含糊。
我的解释是……
您认为 A
和 a
是同一个角色
- 所以,你应该使用
ILIKE
而不是 LIKE
您想要包含两个 A
或 a
的名称,但不要超过两个,并且它们是否相邻并不重要。
WITH
xclient AS
(
SELECT 'Baat' AS client
UNION ALL SELECT 'Marta'
UNION ALL SELECT 'Alaia'
UNION ALL SELECT 'Angalaia'
UNION ALL SELECT 'Aana'
UNION ALL SELECT 'Arcanjamana'
)
SELECT
*
FROM
xclient
WHERE
client ILIKE '%a%a%'
AND client NOT ILIKE '%a%a%a%'
演示:https://dbfiddle.uk/?rdbms=postgres_14&fiddle=a0894fdd211087db552537e40d28d9bf
我如何 select 任何名字中有两个字母“a”的人,在 PostgreSQL 数据库中?
我尝试 select client from xclient where client like '%a%'
但这个 return 所有名称中包含“a”的人。我一定要用“赞”。
结果中是否应包括名字中带有两个“a”的人 示例:“玛尔塔”。 例如,不能包含“Alaia”。
试试这个:
select client from xclient where client like '%a%a%'
我几乎可以肯定此代码在您所说的示例中有效:
select client from xclient where client like '%a%a%' and client not like '%a%a%a%' and client not like '%a%aa%' and client not like '%aa%a%' and client not like '%aa%aa%'
毫无疑问,您可以使用 the substring keyword 优化此代码,但是,无论如何,这应该会如您所愿地执行。
你的问题和评论很含糊。
我的解释是……
您认为 A
和 a
是同一个角色
- 所以,你应该使用
ILIKE
而不是LIKE
您想要包含两个 A
或 a
的名称,但不要超过两个,并且它们是否相邻并不重要。
WITH
xclient AS
(
SELECT 'Baat' AS client
UNION ALL SELECT 'Marta'
UNION ALL SELECT 'Alaia'
UNION ALL SELECT 'Angalaia'
UNION ALL SELECT 'Aana'
UNION ALL SELECT 'Arcanjamana'
)
SELECT
*
FROM
xclient
WHERE
client ILIKE '%a%a%'
AND client NOT ILIKE '%a%a%a%'
演示:https://dbfiddle.uk/?rdbms=postgres_14&fiddle=a0894fdd211087db552537e40d28d9bf