tags:

views:

62

answers:

4

I have columns in a mysql table that stores names of people as combinations of strings and incremented digits for uniqueness, so I have names stored as so :

Patrick, Patrick1, Patrick2, ..... Patrick10, David, David2, .... David5

How do I retrieve just the alpha name itself, without the digits? Say I want to group by the distinct names, and count per group, so I get a result resembling the following.

name    | frequency
-----------------
Patrick | 10
David   | 5
A: 

I'll have a little think about that, but I would recommend that if you need a distinguishing number, you keep it in a different column. That way, you won't have difficulties of this sort.

Brian Hooper
I am not maintaining that table, and I don't have access to the code that populates the columns, hence my request
Olaseni
You have my deepest sympathy, Olaseni.
Brian Hooper
A: 

you could use a udf.

and then try Something like follwing

select REGEX_REPLACE(name, [0-9], '') as Name, Count(Name) 
      from tableName  
      Group by Name
Salil
Didn't work for me
Olaseni
I think that's Oracle and PostgreSQL; I can't find it in the MySQL manual.
Brian Hooper
A MySQL version of REGEXP_REPLACE is available as a UDF from https://launchpad.net/mysql-udf-regexp
Mark Baker
A: 

You can "chain" the replace command like this (this will remove the digits 0,1,2 in the query). You can expand this for the other digits, but I don't know if this will perform very well on large datasets:

select replace(replace(replace(Name,"0",""),"1",""),"2","") from users;

I would think also, it will be better to do what Brian suggested.

Volker
How do you ensure that only appended digits are removed?
Olaseni
I was about to say, Volker had cracked it. I think you'll have to create your own stored function to process the name. More information in 12.1.15 of the MySQL Reference Manual if you need it.
Brian Hooper
+1  A: 

A solution would be this:(it doesn't look to good, but it works)

SELECT 
  TRIM(TRAILING '0' FROM 
    TRIM(TRAILING '1' FROM 
      TRIM(TRAILING '2' FROM 
        TRIM(TRAILING '3' FROM 
          -- ... 
            TRIM(TRAILING '8' FROM 
              TRIM(TRAILING '9' FROM name)))))) AS name
FROM your_table

Then you can select with GROUP BY from the result:

SELECT name, count(*) AS frequency FROM (
-- previous select
) AS t
GROUP BY name
True Soft
That'd be OK provided the numbers don't reach as high as 21.
Brian Hooper
It also fails for 'Patrick10'.
Mark Byers
Whoops! Then the digits should be replaced to a single one and then use TRIM.
True Soft