select distinct user, host, sort from (
select 1, user, host, sort from user
where host not regexp '\%' and user != ''
union
select 2, user, host, sort from user
where host not regexp '\%'
union
select 3, user, host, sort from user
where host not regexp '^\%' and user != ''
union
select 4, user, host, sort from user
where host not regexp '^\%'
union
select 5, user, host, sort from user
where host regexp '^\%.+$' and host not regexp '\%$' and user != ''
union
select 6, user, host, sort from user
where host regexp '^\%.+\%$' and user != ''
union
select 7, user, host, sort from user
where host = '%' and user != ''
union
select 8, user, host, sort from user
where host = '%'
union
select 9, user, host, sort from user
where host regexp '^\%.+$' and host not regexp '\%$'
union
select 10, user, host, sort from user
where host regexp '^\%.+$'
order by 1
) as a;