This is easier than I thought. You just need to group by country, then use MIN().
This would be much simpler, although less error prone, if you had a country code column for each field, instead of analyzing the country text, which could lead to errors.
SELECT t2.country, MIN(CAST(t1.prefix AS SIGNED)) AS prefix FROM MyTable t1
LEFT JOIN MyTable t2
ON t2.prefix = t1.prefix
GROUP BY
IF(
INSTR(t1.country, ' mobile') = 0 AND INSTR(t1.country, '-') = 0,
t1.country,
IF(
INSTR(t1.country, ' mobile') > 0 AND INSTR(t1.country, '-') > 0,
IF(
INSTR(t1.country, ' mobile') > INSTR(t1.country, '-'),
LEFT(t1.country, INSTR(t1.country, '-') - 1),
LEFT(t1.country, INSTR(t1.country, ' mobile') - 1)
),
IF(
INSTR(t1.country, ' mobile') > INSTR(t1.country, '-'),
LEFT(t1.country, INSTR(t1.country, ' mobile') - 1),
LEFT(t1.country, INSTR(t1.country, '-') - 1)
)
)
)
ORDER BY t2.country
Productivity:
country prefix
Argentina-Mobile 549
Armenia 374
Aruba 297
Ascension Island 247
Australia 61
source
share