0

我正在尝试为我的公司过滤掉我的 Excel 客户表中的数据。我需要的三个字段是FIRST_NAMELAST_NAMECOMPANY_NAME

规则如下:

  • FIRST_NAME并且LAST_NAME必须NOTNULL
  • FIRST_NAMEANDLAST_NAME只能是字母
  • COMPANY_NAME如果是,上述规则无关紧要NOT NULL

所以,重申一下。客户必须有一个FIRST_NAMEAND a LAST_NAME(他们不能缺少一个或两个),但是,如果他们有 aCOMPANY_NAME他们被允许没有 aFIRST_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 '%^[A-z]+$%' AND LAST_NAME LIKE '%^[A-z]+$%'))
)

我也尝试过'%[^a-z]%'. 我试过RLIKEand REGEXP,而不是LIKE, 而那些似乎也不起作用。

使用上述查询,结果仅显示带有COMPANY_NAME.

4

2 回答 2

2

修复了使用REGEXP_LIKE和 regex的问题^[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]+$')))
)
于 2016-12-16T17:33:43.723 回答
0

鉴于您提到 RLIKE 和 REGEXP,您似乎正在使用 MySQL。在这种情况下,试试这个 WHERE 子句,它使用正则表达式字符类 'alpha'

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:]]+' 
      ) 
于 2016-12-15T18:53:31.580 回答