Reputation: 20062
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')
AND domainname like ('%.%.%');
Now, I need to order the results by the .%.% part of the domain name and not %.%.%. How can I add this to my query ?
Upvotes: 1
Views: 15766
Reputation: 9335
Check out the SUBSTRING_INDEX()
function:
select domainname from db.table
where criteria like ('*.com')
AND domainname like ('%.%.%')
ORDER BY SUBSTRING_INDEX(domainname, '.', -2);
Upvotes: 4
Reputation: 160833
You could use the function SUBSTRING_INDEX
ORDER BY SUBSTRING_INDEX(domainname, '.', -2)
Upvotes: 2