查询 WHERE 只有字母字符
Query WHERE Only Alphabetic Characters
我正在尝试过滤掉我公司 Excel sheet 客户中的数据。
我需要的三个字段是 FIRST_NAME
、LAST_NAME
和 COMPANY_NAME
.
规则如下:
FIRST_NAME
AND LAST_NAME
必须 NOT
为 NULL
FIRST_NAME
AND LAST_NAME
只能是字母
- 如果
COMPANY_NAME
是 NOT NULL
,则以上规则无关紧要
所以,再次重申清楚.. 客户必须有 FIRST_NAME
和 LAST_NAME
(他们不能缺少一个或两个),但是,如果他们有 COMPANY_NAME
他们被允许没有 FIRST_NAME
and/or LAST_NAME
.
这是一些示例数据,以及它们是否应该保留在数据中:
FIRST_NAME | LAST_NAME | COMPANY_NAME | Good customer?
-----------|-----------|--------------|--------------------------------
Alex | Goodman | AG Inc. | Yes - All are filled out
John | Awesome | | Yes - First and last are fine
Cindy | | Cindy Corp. | Yes - Company is filled out
| | Blank Spa | Yes - Company is filled out
| | | No - Nothing is filled out
Gordon | Mang#2 | | No - Last contains non-alphabet
Jesse#5 | Levvitt | JL Inc. | Yes - Company is filled out
Holly | | | No - No last or company names
这是查询(删除了 SELECT
子句中的一些字段):
SELECT VR_CUSTOMERS.CUSTOMER_ID, VR_CUSTOMERS.FIRST_NAME, VR_CUSTOMERS.LAST_NAME, VR_CUSTOMERS.COMPANY_NAME, ...
FROM DEV.VR_CUSTOMERS VR_CUSTOMERS
WHERE (
LENGTH(NAME)>4 AND
(UPPER(NAME) NOT LIKE UPPER('%delete%')) AND
(COMPANY_NAME IS NOT NULL OR (COMPANY_NAME IS NULL AND FIRST_NAME IS NOT NULL AND LAST_NAME IS NOT NULL AND FIRST_NAME LIKE '%^[A-z]+$%' AND LAST_NAME LIKE '%^[A-z]+$%'))
)
我也试过 '%[^a-z]%'
的正则表达式。我试过 RLIKE
和 REGEXP
,而不是 LIKE
,但它们似乎也不起作用。
使用上述查询,结果仅显示具有 COMPANY_NAME
.
的记录
鉴于您提到的 RLIKE 和 REGEXP,您似乎正在使用 MySQL。在这种情况下,试试这个使用 regular expression character class 'alpha' 的 WHERE 子句:
WHERE
COMPANY_NAME is not null -- COMPANY_NAME being present is the higher priority pass condition
or ( -- but if COMPANY_NAME is not present, then the following conditions must be satisfied
FIRST_NAME is not null
and FIRST_NAME REGEXP '[[:alpha:]]+'
and LAST_NAME is not null
and LAST_NAME REGEXP '[[:alpha:]]+'
)
请记住,给定正则表达式,非空检查是多余的,因此 WHERE 子句将自身简化为:
WHERE
COMPANY_NAME is not null -- COMPANY_NAME being present is the higher priority pass condition
or ( -- but if COMPANY_NAME is not present, then the following conditions must be satisfied
FIRST_NAME REGEXP '[[:alpha:]]+'
and LAST_NAME REGEXP '[[:alpha:]]+'
)
使用 REGEXP_LIKE
和正则表达式 ^[A-z]+$
解决了问题。
这是此修复后的 WHERE
子句:
WHERE (
LENGTH(NAME)>4 AND
(UPPER(NAME) NOT LIKE UPPER('%delete%')) AND
(COMPANY_NAME IS NOT NULL OR (COMPANY_NAME IS NULL AND REGEXP_LIKE(FIRST_NAME, '^[A-z]+$') AND REGEXP_LIKE(LAST_NAME, '^[A-z]+$')))
)
我正在尝试过滤掉我公司 Excel sheet 客户中的数据。
我需要的三个字段是 FIRST_NAME
、LAST_NAME
和 COMPANY_NAME
.
规则如下:
FIRST_NAME
ANDLAST_NAME
必须NOT
为NULL
FIRST_NAME
ANDLAST_NAME
只能是字母- 如果
COMPANY_NAME
是NOT NULL
,则以上规则无关紧要
所以,再次重申清楚.. 客户必须有 FIRST_NAME
和 LAST_NAME
(他们不能缺少一个或两个),但是,如果他们有 COMPANY_NAME
他们被允许没有 FIRST_NAME
and/or LAST_NAME
.
这是一些示例数据,以及它们是否应该保留在数据中:
FIRST_NAME | LAST_NAME | COMPANY_NAME | Good customer?
-----------|-----------|--------------|--------------------------------
Alex | Goodman | AG Inc. | Yes - All are filled out
John | Awesome | | Yes - First and last are fine
Cindy | | Cindy Corp. | Yes - Company is filled out
| | Blank Spa | Yes - Company is filled out
| | | No - Nothing is filled out
Gordon | Mang#2 | | No - Last contains non-alphabet
Jesse#5 | Levvitt | JL Inc. | Yes - Company is filled out
Holly | | | No - No last or company names
这是查询(删除了 SELECT
子句中的一些字段):
SELECT VR_CUSTOMERS.CUSTOMER_ID, VR_CUSTOMERS.FIRST_NAME, VR_CUSTOMERS.LAST_NAME, VR_CUSTOMERS.COMPANY_NAME, ...
FROM DEV.VR_CUSTOMERS VR_CUSTOMERS
WHERE (
LENGTH(NAME)>4 AND
(UPPER(NAME) NOT LIKE UPPER('%delete%')) AND
(COMPANY_NAME IS NOT NULL OR (COMPANY_NAME IS NULL AND FIRST_NAME IS NOT NULL AND LAST_NAME IS NOT NULL AND FIRST_NAME LIKE '%^[A-z]+$%' AND LAST_NAME LIKE '%^[A-z]+$%'))
)
我也试过 '%[^a-z]%'
的正则表达式。我试过 RLIKE
和 REGEXP
,而不是 LIKE
,但它们似乎也不起作用。
使用上述查询,结果仅显示具有 COMPANY_NAME
.
鉴于您提到的 RLIKE 和 REGEXP,您似乎正在使用 MySQL。在这种情况下,试试这个使用 regular expression character class 'alpha' 的 WHERE 子句:
WHERE
COMPANY_NAME is not null -- COMPANY_NAME being present is the higher priority pass condition
or ( -- but if COMPANY_NAME is not present, then the following conditions must be satisfied
FIRST_NAME is not null
and FIRST_NAME REGEXP '[[:alpha:]]+'
and LAST_NAME is not null
and LAST_NAME REGEXP '[[:alpha:]]+'
)
请记住,给定正则表达式,非空检查是多余的,因此 WHERE 子句将自身简化为:
WHERE
COMPANY_NAME is not null -- COMPANY_NAME being present is the higher priority pass condition
or ( -- but if COMPANY_NAME is not present, then the following conditions must be satisfied
FIRST_NAME REGEXP '[[:alpha:]]+'
and LAST_NAME REGEXP '[[:alpha:]]+'
)
使用 REGEXP_LIKE
和正则表达式 ^[A-z]+$
解决了问题。
这是此修复后的 WHERE
子句:
WHERE (
LENGTH(NAME)>4 AND
(UPPER(NAME) NOT LIKE UPPER('%delete%')) AND
(COMPANY_NAME IS NOT NULL OR (COMPANY_NAME IS NULL AND REGEXP_LIKE(FIRST_NAME, '^[A-z]+$') AND REGEXP_LIKE(LAST_NAME, '^[A-z]+$')))
)