pryazhnikov / mysql-info-queries

Some useful mysql queries
MIT License
1 stars 0 forks source link

Improve table prefix detection #1

Open pryazhnikov opened 6 years ago

pryazhnikov commented 6 years ago

Right now tables.sql has this condition:

SUBSTRING_INDEX(TABLE_NAME, "_", 1)

This condition works well against table names like 'user_photos_123', but fails against other variations:

SELECT SUBSTRING_INDEX("Users123", "_", 1);
# => Users123

SELECT SUBSTRING_INDEX("UserPhotos123", "_", 1);
# => UserPhotos123

SELECT SUBSTRING_INDEX("user_photos_123", "_", 1);
# => user
pryazhnikov commented 6 years ago

There is an obvious solution at MySQL 8.0:

## "TABLE_NAME_123" => "TABLE_NAME_"
SELECT REGEXP_REPLACE("TABLE_NAME_123", "[0-9]+$", "");