[英]Query WHERE Only Alphabetic Characters
I am trying to filter out data in my Excel sheet of customers for my company. 我正在尝试过滤出我公司的Excel客户表中的数据。 The three fields I need to by are
FIRST_NAME
, LAST_NAME
, and COMPANY_NAME
. 我需要的三个字段是
FIRST_NAME
, LAST_NAME
和COMPANY_NAME
。
The rules are as follows: 规则如下:
FIRST_NAME
AND LAST_NAME
must NOT
be NULL
FIRST_NAME
和LAST_NAME
NOT
为NULL
FIRST_NAME
AND LAST_NAME
must be only alphabetic FIRST_NAME
和LAST_NAME
只能是字母 COMPANY_NAME
is NOT NULL
COMPANY_NAME
NOT NULL
,则上述规则不相关 So, just to reiterate to be clear.. A customer must have a FIRST_NAME
AND a LAST_NAME
(They cannot be missing one or both), BUT, if they have a COMPANY_NAME
they are allowed to not have a FIRST_NAME
and/or LAST_NAME
. 因此,请重申一下。客户必须具有
FIRST_NAME
和LAST_NAME
(他们不能缺少一个或两个),但是,如果他们具有COMPANY_NAME
,则他们不能具有FIRST_NAME
和/或LAST_NAME
。
Here's some example data and if they should stay in the data or not: 以下是一些示例数据,以及是否应保留在数据中:
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
Here is the query (With some fields in the SELECT
clause removed): 这是查询(删除了
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]+$%'))
)
I've tried as well the regex of '%[^az]%'
. 我也尝试过
'%[^az]%'
的正则表达式。 I've tried RLIKE
and REGEXP
, instead of LIKE
, and those did not seem to work either. 我已经尝试了
RLIKE
和REGEXP
,而不是LIKE
,但是它们似乎也不起作用。
With the above query, the results only show records with a COMPANY_NAME
. 对于上述查询,结果仅显示具有
COMPANY_NAME
记录。
Fixed the issue using REGEXP_LIKE
and the regex ^[Az]+$
. 使用
REGEXP_LIKE
和regex ^[Az]+$
解决了该问题。
Here is the WHERE
clause after this fix: 此修复后的
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]+$')))
)
It appears you're using MySQL given your mention of RLIKE and REGEXP. 提到RLIKE和REGEXP,您似乎正在使用MySQL。 In that case, try this WHERE clause, that uses the regular expression character class 'alpha' :
在这种情况下,请尝试使用正则表达式字符类'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:]]+'
)
Bear in mind that the not null check is redundant given the regular expression, so the WHERE clause would simplify itself to: 请记住,在给定正则表达式的情况下,not null检查是多余的,因此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:]]+'
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.