REGEXP_REPLACE с DISTINCT - PullRequest
       10

REGEXP_REPLACE с DISTINCT

0 голосов
/ 02 февраля 2019

Я пытаюсь использовать разные с REGEXP_REPLACE, и возвращается 0 строк.

Я создал тестовую таблицу в MySQLP v8.0

   CREATE TABLE phone(
   id serial primary key,
   phone_number char(25));

   INSERT INTO phone (phone_number)
   VALUES ('(423) 330-9999');

   INSERT INTO phone (phone_number)
   VALUES ('(423)3309999');

   INSERT INTO phone (phone_number)
   VALUES ('423-330-1111)');

   INSERT INTO phone (phone_number)
   VALUES ('1-423-330-6666');

   INSERT INTO phone (phone_number)
   VALUES ('1A423*330*1111');

   INSERT INTO phone (phone_number)
   VALUES ('5553301111');

- Тогда

select 
   REGEXP_REPLACE(phone_number, '[^0-9]', '',1, 0, 'm') as clean_phone
from phone

--- отлично работает -> clean_phone 4233309999 4233309999 4233301111 14233306666 14233301111 5553301111

--- count

select 
   count(REGEXP_REPLACE(phone_number, '[^0-9]', '',1, 0, 'm')) as 
   clean_phone
from phone

--- отлично работает -> clean_phone 6

- отличный clean_phone

select 
   distinct(REGEXP_REPLACE(phone_number, '[^0-9]', '',1, 0, 'm')) as 
   clean_phone
from phone

--- возвращает пустое значение -> clean_phone

Я не понимаю, почему отличный не работает?

1 Ответ

0 голосов
/ 02 февраля 2019

Distinct - это не функция, поэтому вам не нужен Отдельный (), а только отдельный

select  distinct REGEXP_REPLACE(phone_number, '[^0-9]', '',1, 0, 'm') as 
   clean_phone
from phone

.

    select distinct  clear_phone from(
    select   REGEXP_REPLACE(phone_number, '[^0-9]', '',1, 0, 'm')  clear_phone
   from phone ) t 

Если ошибки все еще остаются, вы можете попробовать использовать вставку/ выбор из фиктивного стола

  insert into dummy_table(clear_phone)
  select   REGEXP_REPLACE(phone_number, '[^0-9]', '',1, 0, 'm')  
  from phone;

  select distinct clear_phone from dummy_table;
...