简体   繁体   中英

Get unique values from MySql

In my table are values like: 14, 15, 14, R14, R15, R15C, R14C,....

With current distinct mode I get: 14, R14, R14C, R15, R15C,...

But what I want is only unique numbers like this: 14, 15,...

To get rid of letters and get unique numbers only?

My current code:

SELECT DISTINCT diameter FROM tires WHERE category = '$cat' ORDER BY diameter ASC
SELECT DISTINCT 0 + REGEXP_REPLACE(diameter, '^[[:alpha:]]*', '') diameter
FROM tires;

Regular expression removes all letter chars from the beginning, then the value is implicitly converted to numeric by adding zero (by fact - trailing letters are removed).

fiddle


I forget to tell that I have decimal values like R22,5 - RoX

Replace comma with dot before or after REGEXP_REPLACE(), but before adding zero. For example,

SELECT DISTINCT 0 + REGEXP_REPLACE(REPLACE(diameter, ',', '.'), '^[[:alpha:]]*', '') diameter
FROM tires;

fiddle

PS. I'd recommend to add generated column to the table structure. See example .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM