I have the following query to select the domain names that have three levels separated by two dots:
select domainname from db.table where criteria like (\'*.com\
You could use the function SUBSTRING_INDEX
ORDER BY SUBSTRING_INDEX(domainname, '.', -2)
Check out the SUBSTRING_INDEX() function:
select domainname from db.table where criteria like ('*.com') AND domainname like ('%.%.%') ORDER BY SUBSTRING_INDEX(domainname, '.', -2);