查询只有字母字符

我正试图过滤掉我公司的Excel表单中的数据。 我需要的三个字段是FIRST_NAMELAST_NAMECOMPANY_NAME

规则如下:

  • FIRST_NAMELAST_NAME NOTNULL
  • FIRST_NAMELAST_NAME只能是字母
  • 上面的规则是不相关的,如果COMPANY_NAME NOT NULL

所以,重申要清楚一点。客户必须拥有FIRST_NAMELAST_NAME (他们不能缺less一个或两个),但是,如果他们有一个COMPANY_NAME他们可以没有FIRST_NAME和/或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 '%^[Az]+$%' AND LAST_NAME LIKE '%^[Az]+$%')) ) 

我也尝试了'%[^az]%'的正则expression式。 我试过RLIKEREGEXP ,而不是LIKE ,而那些似乎也没有工作。

通过上面的查询,结果只显示一个COMPANY_NAMElogging。

修复了使用REGEXP_LIKE和正则expression式^[Az]+$

这是修复后的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, '^[Az]+$') AND REGEXP_LIKE(LAST_NAME, '^[Az]+$'))) ) 

看来你正在使用MySQL给你提到的RLIKE和REGEXP。 在这种情况下,请尝试使用正则expression式字符类“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:]]+' ) 

请记住,对于正则expression式来说,非空检查是多余的,所以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:]]+' )